Next: Main Subprograms, Previous: Source Files and Directories, Up: Building With Projects
The next step when writing a project is to indicate where the compiler should put the object files. In fact, the compiler and other tools might create several different kind of files (for GNAT, there is the object file and the ALI file for instance). One of the important concepts in projects is that most tools may consider source directories as read-only and do not attempt to create new or temporary files there. Instead, all files are created in the object directory. It is of course not true for project-aware IDEs, whose purpose it is to create the source files.
The object directory is specified through the Object_Dir attribute.
Its value is the path to the object directory, either absolute or
relative to the directory containing the project file. This
directory must already exist and be readable and writable, although
some tools have a switch to create the directory if needed (See
the switch -p
for gnatmake and gprbuild).
If the attribute Object_Dir
is not specified, it defaults to
the project directory, that is the directory containing the project file.
For our example, we can specify the object dir in this way:
project Build is for Source_Dirs use ("common"); for Object_Dir use "obj"; -- <<<< end Build;
As mentioned earlier, there is a single object directory per project. As a result, if you have an existing system where the object files are spread in several directories, you can either move all of them into the same directory if you want to build it with a single project file, or study the section on subsystems (see Organizing Projects into Subsystems) to see how each separate object directory can be associated with one of the subsystem constituting the application.
When the linker is called, it usually creates an executable. By default, this executable is placed in the object directory of the project. It might be convenient to store it in its own directory.
This can be done through the Exec_Dir
attribute, which, like
Object_Dir contains a single absolute or relative path and must point to
an existing and writable directory, unless you ask the tool to create it on
your behalf. When not specified, It defaults to the object directory and
therefore to the project file's directory if neither Object_Dir nor
Exec_Dir was specified.
In the case of the example, let's place the executable in the root of the hierarchy, ie the same directory as build.gpr. Hence the project file is now
project Build is for Source_Dirs use ("common"); for Object_Dir use "obj"; for Exec_Dir use "."; -- <<<< end Build;