Next: , Up: Mixed Language Programming


2.10.1 Interfacing to C

There are two ways to build a program that contains some Ada files and some other language files depending on whether the main program is in Ada or not. If the main program is in Ada, you should proceed as follows:

  1. Compile the other language files to generate object files. For instance:
              gcc -c file1.c
              gcc -c file2.c
         
  2. Compile the Ada units to produce a set of object files and ALI files. For instance:
              gnatmake -c my_main.adb
         
  3. Run the Ada binder on the Ada main program. For instance:
              gnatbind my_main.ali
         
  4. Link the Ada main program, the Ada objects and the other language objects. For instance:
              gnatlink my_main.ali file1.o file2.o
         

The three last steps can be grouped in a single command:

     gnatmake my_main.adb -largs file1.o file2.o

If the main program is in some language other than Ada, Then you may have more than one entry point in the Ada subsystem. You must use a special option of the binder to generate callable routines to initialize and finalize the Ada units (see Binding with Non-Ada Main Programs). Calls to the initialization and finalization routines must be inserted in the main program, or some other appropriate point in the code. The call to initialize the Ada units must occur before the first Ada subprogram is called, and the call to finalize the Ada units must occur after the last Ada subprogram returns. You use the same procedure for building the program as described previously. In this case, however, the binder only places the initialization and finalization subprograms into file b~xxx.adb instead of the main program. So, if the main program is not in Ada, you should proceed as follows:

  1. Compile the other language files to generate object files. For instance:
              gcc -c file1.c
              gcc -c file2.c
         
  2. Compile the Ada units to produce a set of object files and ALI files. For instance:
              gnatmake -c entry_point1.adb
              gnatmake -c entry_point2.adb
         
  3. Run the Ada binder on the Ada main program. For instance:
              gnatbind -n entry_point1.ali entry_point2.ali
         
  4. Link the Ada main program, the Ada objects and the other language objects. You only need to give the last entry point here. For instance:
              gnatlink entry_point2.ali file1.o file2.o