如何在GPR项目文件中指定可执行文件名?能否输出到上级目录?
I'm working with an Ada project using GNAT's GPR build system, and I need to customize where my executable ends up and what it's named. My current project file looks like this:
project some_project is for Source_Dirs use ("src"); for Object_Dir use ("obj"); for Main use ("main.adb"); package Compiler is for Default_Switches ("ada") use ("-O1", "-gnatwae"); end Compiler; end some_project;
Right now, it generates an executable named main.exe in the project's root directory. I want instead to output ../some_program_name.exe — so changing both the executable's name and sending it to the parent directory. How do I configure this in the GPR file?
Great question! You can handle both requirements directly in your GPR project file using two key attributes: Executable_Name and Executable_Dir. Here's the updated project code that does exactly what you need:
project some_project is for Source_Dirs use ("src"); for Object_Dir use ("obj"); for Main use ("main.adb"); -- Send the executable to the parent directory for Executable_Dir use "../"; -- Set a custom name for the executable for Executable_Name use "some_program_name"; package Compiler is for Default_Switches ("ada") use ("-O1", "-gnatwae"); end Compiler; end some_project;
Let me break down each part:
Executable_Dir: This attribute defines the output directory for your final executable. Using"../"tells GNAT to place the file in the directory one level above your project file's location.Executable_Name: This overrides the default executable name (which is normally the base name of yourMainfile — somainin this case). Setting it to"some_program_name"will producesome_program_name.exeon Windows (or justsome_program_nameon Unix-like systems).
You can also use these attributes independently if you only need one of the changes:
- If you just want a custom name but keep the executable in the default location, skip the
Executable_Dirline. - If you just want to move the default-named executable to the parent directory, only include the
Executable_Dirline.
One quick heads-up: Make sure the parent directory exists before building. GNAT won't create it automatically, so you'll need to make it manually or add a post-build step if you want it created dynamically.
内容的提问来源于stack exchange,提问作者TamaMcGlinn

