In this tutorial we will create and build a fully executable (simple) project. We have chosen to create a project for an embedded computer because it highlights some of the additional capabilities provided by GNATbench; the process is almost exactly the same for creating executables for native execution on the host computer.
Creating a new project for Ada development is easy with GNATbench. The wizard will create and configure the project for us, and invoking the builder is simply a matter of selecting a command.
The first step is to invoke the wizard. Using the Ada toolbar addition, click on the arrow next to the new-project icon and select "Ada Project with Objects Folder", as shown in the following figure:
The first page of the new-project wizard (shown in the next figure) will appear. Enter the name of the new project. We have named this one "ppc_elf_demo". We have the option of locating the new project in the default workspace or elsewhere in the file system. We'll take the default. Press Next.
In the next page we enter the name of the Ada main subprogram -- not the file name -- and have the wizard generate the file containing that unit. We arbitrarily name the unit "hello" and have it generated as a program that prints "Hello World!" when executed.
On the next page we then choose to use the PowerPC Elf compiler by selecting it from the list of pre-configured builders. This selection will enable the Finish button.
Press Finish. The new project will be created and you will see it in the GNAT Project Explorer, like so:
Note the presence of the files named "Makefile" and "ppc_elf_demo.gpr". These are the makefile and GNAT project file, respectively. These files may be edited but they must not be deleted.
Also note the file containing the main subprogram underneath the source folder (we expanded it to show the file). This file will have the name we specified, "hello.adb", and will contain a procedure with that unit name:
In a real application we would alter the content of the main subprogram. It is a buildable subprogram, though, without any changes, so for the purpose of this tutorial we can leave it as it is.
We need a linker script to link the executable because this is a bare-board application. Hence we must tell the linker how to find this script so we modify the GNAT project created by the wizard.
The original project file looks like this:
We then add the Linker package for the sake of the script:
We've also added a reference to an object file named "putchar.o" that will be explained momentarily.
We also have to insert this script into the workspace project. We have a stored copy on disk so an easy way to get it into the project is to use the Import... wizard from the Navigator and get the script from the file system.
We also need to import an assembly language source file. This assembly source file contains startup code necessary for the application to run on the bare machine.
Similarly, we need the source files for an Ada procedure that defines how to output a character using the hardware on the bare board. That Ada procedure is the "putchar" routine mentioned earlier in the Linker package. The source files are located in the "extra_rtl" subdirectory.
We import all of them by selecting "File System" under the "General" category and pressing Next.
After browsing to the location and selecting the two individual files required, we also select the directory named "extra_rtl" so that the putchar sources will be imported too. Then simply press Finish and the files will be imported into the project.
Now that we have the files, we need to compile them, once, before building the entire application. We will add a new command and name it "setup" since we only need to compile the source files once. In practice we would have the Makefile automatically invoke this command when necessary, but we will do it manually for the sake of this tutorial.
Therefore, we modify our "Makefile" file to add a "setup" command. This command will be added to the bottom of the Makefile:
Note the comment above the target name. This comment will appear in the dialog box to serve as a reminder of what the new command does.
We can now invoke the Project menu (or the Navigator's contextual menu) and choose "Invoke Makefile Target" to get the list of user-defined target commands available.
The list of all predefined and user-defined commands appears:
Select the name "setup" among the listed build target names and press Invoke. Command execution will be displayed in the console.
If we then expand the "objs" directory in the GNAT Project Explorer we see the resulting files:
Now that the setup has completed we are ready to do the full build. In the Eclipse menubar, open the Project menu and select "Build Current Project".
The build will then proceed and we can watch it in the build console. In this case we see in the console that the build failed due to an unresolved symbol named "_gnat_stack_check".
This particular run-time library does not include stack checking routines. Recall that the project file from the wizard included this option, among others, in the default switches of package Compiler:
We must edit the project file to remove the "-fstack-check" switch from the package Compiler. (Alternatively, one can invoke Edit GNAT Project Properties to use the graphical interface.) After you remove the switch be sure to save the changes to the project file.
We can then rebuild the project. Open the Project menu and select "Rebuild Current Project":
A successful rebuild will look like the following:
That's it! You have created and built a project for an embedded computer using GNATbench for Eclipse.