comp.lang.ada
 help / color / mirror / Atom feed
* Link C main with Ada static library
@ 2002-11-29  8:45 Jeff
  2002-11-29 10:04 ` sk
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff @ 2002-11-29  8:45 UTC (permalink / raw)


I've been trying to get a C main program to link to an Ada static
library using Gnat on Linux.  In order to simplify the task of
figuring out the correct process, I created a simple Ada static
library that exports a single function having no parameters and that
returns the integer "1".

I've been able to generate an Ada program which successful calls the
function of the static library.  But, I'm having no luck getting a
simple C main program to link to the Ada static library.  I hope the
below sheds some light. and that someone will provide a simple working
example or suggestions as to what i'm missing (I suspect 1 or more
switches). I have reveiwed the Gnat and Library section of  the Users
Manual.  But the manual doesn't show the process of linking the C main
to the Ada library.

$ gcc -ladazlib -L./ main.c
/tmp/cco0XQDo.o(.text+0x11): In function `main':
:undefined reference to `adazlibinit'
/tmp/cco0XQDo.o(.text+0x11): In function `main':
:undefined reference to `adaztest'
/tmp/cco0XQDo.o(.text+0x11): In function `main':
:undefined reference to `adazlibfinal'
collect2: ld returned 1 exit status

$ nm -s libadazlib.a

Archive index:
adazlibmain_E in b~adaztest.o
adazlibinit in b~adatest.o
adazlibfinal in b~adaztest.o
adazlib_E in adazlib.o
adaztest in adazlib.o
....

From the above nm output it appears that the symbols are in the Ada
library.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Link C main with Ada static library
  2002-11-29  8:45 Link C main with Ada static library Jeff
@ 2002-11-29 10:04 ` sk
  2002-11-29 22:27   ` Jeff
  0 siblings, 1 reply; 4+ messages in thread
From: sk @ 2002-11-29 10:04 UTC (permalink / raw)


Hi,

Tricky without seeing your "library" code, but this
works, GNAT 3.15p, Linux-Mandrake 8.2.

-- static_ada_library.ads --
package Static_Ada_Library is

     function Return_Forty_Two return Natural;
     pragma Export (C, Return_Forty_Two, "return_forty_two");

end Static_Ada_Library;

-- static_ada_library.adb --
package body Static_Ada_Library is

     function Return_Forty_Two return Natural is
     begin
         return 42;
     end Return_Forty_Two;

end Static_Ada_Library;

-- main.c --
extern int return_forty_two ();
extern int adainit ();
extern int adafinal ();

#include <stdio.h>

int main () {

     int i = 0;

     printf ("i => %d\n", i);


     adainit ();

     i = return_forty_two ();

     adafinal ();

     printf ("i => %d\n", i);

};


Now the build ...

# gnatmake -c static_ada_library
# gnatbind -n static_ada_library

-- OR gnatbind -z ...

# gcc -c b~static_ada_library.adb
# ar rc static_ada_library.a static_ada_library.o b~static_ada_library.o
# gcc main.c static_ada_library.a -o c-test \
/usr/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a

# ./c-test
i => 0
i => 42

-- 
The above is very CRUDE, but it should point you in the right
direction.

Review your "ld" manual to see how to link against
"libgnat.so" etc.

Good luck.


-- For real address, merge vertically
-------------------
-- s n p @ t . o
--  k i e k c c m
-------------------




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Link C main with Ada static library
  2002-11-29 10:04 ` sk
@ 2002-11-29 22:27   ` Jeff
  2002-11-29 23:25     ` sk
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff @ 2002-11-29 22:27 UTC (permalink / raw)


Thanks.  This seems to do the trick.  However, it doesn't seem to be
completely in accord with the description in the GNAT User's Manual
for generating Ada libraries. In particular, the manual says to use
the -L switch with gnatbind.
Even in light of your solution, I have been unable to get it work
using -L switch of gnatbind as outlined in the manual. But, your
solution seems to work which is all that matters to me at this point.

Thanks again

-- Jeff

sk <sk@noname.com> wrote in message news:<mailman.1038564541.20930.comp.lang.ada@ada.eu.org>...
> Hi,
> 
> Tricky without seeing your "library" code, but this
> works, GNAT 3.15p, Linux-Mandrake 8.2.
> 
> -- static_ada_library.ads --
> package Static_Ada_Library is
> 
>      function Return_Forty_Two return Natural;
>      pragma Export (C, Return_Forty_Two, "return_forty_two");
> 
> end Static_Ada_Library;
> 
> -- static_ada_library.adb --
> package body Static_Ada_Library is
> 
>      function Return_Forty_Two return Natural is
>      begin
>          return 42;
>      end Return_Forty_Two;
> 
> end Static_Ada_Library;
> 
> -- main.c --
> extern int return_forty_two ();
> extern int adainit ();
> extern int adafinal ();
> 
> #include <stdio.h>
> 
> int main () {
> 
>      int i = 0;
> 
>      printf ("i => %d\n", i);
> 
> 
>      adainit ();
> 
>      i = return_forty_two ();
> 
>      adafinal ();
> 
>      printf ("i => %d\n", i);
> 
> };
> 
> 
> Now the build ...
> 
> # gnatmake -c static_ada_library
> # gnatbind -n static_ada_library
> 
> -- OR gnatbind -z ...
> 
> # gcc -c b~static_ada_library.adb
> # ar rc static_ada_library.a static_ada_library.o b~static_ada_library.o
> # gcc main.c static_ada_library.a -o c-test \
> /usr/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
> 
> # ./c-test
> i => 0
> i => 42
> 
> -- 
> The above is very CRUDE, but it should point you in the right
> direction.
> 
> Review your "ld" manual to see how to link against
> "libgnat.so" etc.
> 
> Good luck.
> 
> 
> -- For real address, merge vertically
> -------------------
> -- s n p @ t . o
> --  k i e k c c m
> -------------------



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Link C main with Ada static library
  2002-11-29 22:27   ` Jeff
@ 2002-11-29 23:25     ` sk
  0 siblings, 0 replies; 4+ messages in thread
From: sk @ 2002-11-29 23:25 UTC (permalink / raw)


Hi,

jeff.huter@bigfoot.com :
 > I have been unable to get it work using -L switch of
 > gnatbind as outlined in the manual

I have lost my 3.14p docs and have only just installed 3.15p,
but it seems that the gnatbind "-L" switch is new (undoubtdly
wrong, but I hadn't noticed "-L" for gnstbind before).

... but (following from previous example) ...

-- main.c
extern int return_forty_two ();
extern int mylibinit ();
extern int mylibfinal ();

#include <stdio.h>

int main () {

     int i = 0;

     printf ("i => %d\n", i);

     mylibinit ();

     i = return_forty_two ();

     mylibfinal ();

     printf ("i => %d\n", i);

};

... the build ...

# gnatmake static_ada_library
# gnatbind -Lmylib static_ada_library
# gcc -c b~static_ada_library.adb
# ar cr libmylib.a b~static_ada_library.o static_ada_library.o
# gcc main.c -o c-test -lmylib -L. \
usr/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
# ./c-test
i => 0
i => 42
#


-- For real address, merge vertically
-------------------
-- s n p @ t . o
--  k i e k c c m
-------------------




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-11-29 23:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-29  8:45 Link C main with Ada static library Jeff
2002-11-29 10:04 ` sk
2002-11-29 22:27   ` Jeff
2002-11-29 23:25     ` sk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox