Next: Set compiler options, Up: Compiling Examples [Contents][Index]

### 6.1 No project files

This example uses no project files.

First, create a directory `Example_1`, containing:

`hello.adb`:

    with Ada.Text_IO;
    procedure Hello
    is begin
       Put_Line("Hello from hello.adb");
    end Hello;

Yes, this is missing “use Ada.Text_IO;” - we want to demonstrate compiler error handling.

`hello_2.adb`:

    with Hello_Pkg;
    procedure Hello_2
    is begin
       Hello_Pkg.Say_Hello;
    end Hello_2;

This file has no errors.

`hello_pkg.ads`:

    package Hello_Pkg is
       procedure Say_Hello;
    end Hello_Pkg;

This file has no errors.

`hello_pkg.adb`:

    with Ada.Text_IO;
    package Hello_Pkg is
       procedure Say_Hello
       is begin
          Ada.Text_IO.Put_Line ("Hello from hello_pkg.adb");
       end Say_Hello;
    end Hello_Pkg;

Yes, this is missing the keyword `body`; another compiler error example.

In buffer `hello.adb`, invoke ‘`Ada | Check file`’. You should get a `*compilation*` buffer containing something like (the directory paths will be different):

    cd c:/Examples/Example_1/
    gnatmake -u -c -gnatc -g c:/Examples/Example_1/hello.adb -cargs -gnatq -gnatQ
    gcc -c -Ic:/Examples/Example_1/ -gnatc -g -gnatq -gnatQ -I- c:/Examples/Example_1/hello.adb
    hello.adb:4:04: "Put_Line" is not visible
    hello.adb:4:04: non-visible declaration at a-textio.ads:264
    hello.adb:4:04: non-visible declaration at a-textio.ads:260
    gnatmake: "c:/Examples/Example_1/hello.adb" compilation error

If you have enabled font-lock, the lines with actual errors (starting with `hello.adb`) are highlighted, with the file name in red.

Now type `C-x `` (on a PC keyboard, ``` is next to `1`). Or you can click the middle mouse button on the first error line. The compilation buffer scrolls to put the first error on the top line, and point is put at the place of the error in the `hello.adb` buffer.

To fix the error, change the line to be

        Ada.Text_IO.Put_Line ("hello from hello.adb");

Now invoke ‘`Ada | Show main`’; this displays ‘`Ada mode main: hello`’.

Now (in buffer `hello.adb`), invoke ‘`Ada | Build`’. You are prompted to save the file (if you haven’t already). Then the compilation buffer is displayed again, containing:

    cd c:/Examples/Example_1/
    gnatmake -o hello hello -g -cargs -gnatq -gnatQ -bargs  -largs
    gcc -c -g -gnatq -gnatQ hello.adb
    gnatbind -x hello.ali
    gnatlink hello.ali -o hello.exe -g

The compilation has succeeded without errors; `hello.exe` now exists in the same directory as `hello.adb`.

Now invoke ‘`Ada | Run`’. A `*run*` buffer is displayed, containing

    Hello from hello.adb

    Process run finished

That completes the first part of this example.

Now we will compile a multi-file project. Open the file `hello_2.adb`, and invoke ‘`Ada | Set main and Build`’. This finds an error in `hello_pkg.adb`:

    cd c:/Examples/Example_1/
    gnatmake -o hello_2 hello_2 -g -cargs -gnatq -gnatQ -bargs  -largs
    gcc -c -g -gnatq -gnatQ hello_pkg.adb
    hello_pkg.adb:2:08: keyword "body" expected here [see file name]
    gnatmake: "hello_pkg.adb" compilation error

This demonstrates that gnatmake finds the files needed by the main program. However, it cannot find files in a different directory, unless you use an Emacs Ada mode project file to specify the other directories; See Set source search path, or a GNAT project file; Use GNAT project file.

Invoke ‘`Ada | Show main`’; this displays `Ada mode main: hello_2`.

Move to the error with `C-x ``, and fix the error by adding `body`:

    package body Hello_Pkg is

Now, while still in `hello_pkg.adb`, invoke ‘`Ada | Build`’. gnatmake successfully builds `hello_2`. This demonstrates that Emacs has remembered the main file, in the project variable `main`, and used it for the Build command.

Finally, again while in `hello_pkg.adb`, invoke ‘`Ada | Run`’. The `*run*` buffer displays `Hello from hello_pkg.adb`.

One final point. If you switch back to buffer `hello.adb`, and invoke ‘`Ada | Run`’, `hello_2.exe` will be run. That is because `main` is still set to `hello_2`, as you can see when you invoke ‘`Ada | Project | Edit`’.

There are three ways to change `main`:

1. Invoke ‘`Ada | Set main and Build`’, which sets `main` to the current file.
2. Invoke ‘`Ada | Project | Edit`’, edit `main`, and click ‘`[save]`’
3. Invoke ‘`Ada | Project | Load`’, and load a project file that specifies `main`

Next: Set compiler options, Up: Compiling Examples [Contents][Index]