comp.lang.ada
 help / color / mirror / Atom feed
* Shared library in Ada
@ 2008-04-19 11:27 Sebastien
  2008-04-19 11:41 ` Georg Bauhaus
  2008-04-19 11:52 ` Ludovic Brenta
  0 siblings, 2 replies; 9+ messages in thread
From: Sebastien @ 2008-04-19 11:27 UTC (permalink / raw)


Hi,

I'm a new developper in Ada, and I found out a really suprising and 
unknown language. I have a read a lot of stuff on internet, made many tests.

Now there is only one things I didn't get information about.

I'd like to create a share object in Ada and use it in an Ada software.

So the question is, is the ads file going to be enough to get my program 
compile?

For instance:

with mylibpkg;
procedure Main is
begin
	mylibpkg.Print();
end Main;

I have only the mylibpkg.so and the mylibpkg.ads, can I compile?

ldd main is going to me dependent of mylibpkg.so?

Thanks by advance,
Sebastien




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

* Re: Shared library in Ada
  2008-04-19 11:27 Shared library in Ada Sebastien
@ 2008-04-19 11:41 ` Georg Bauhaus
  2008-04-19 11:51   ` Sebastien
  2008-04-19 11:52 ` Ludovic Brenta
  1 sibling, 1 reply; 9+ messages in thread
From: Georg Bauhaus @ 2008-04-19 11:41 UTC (permalink / raw)


Sebastien wrote:
> Hi,
> 
> I'm a new developper in Ada, and I found out a really suprising and
> unknown language. I have a read a lot of stuff on internet, made many
> tests.
> 
> Now there is only one things I didn't get information about.
> 
> I'd like to create a share object in Ada and use it in an Ada software.

I suppose you are referring to dynamic linking at the OS level?
In this case you need to import functions from the library, perhaps
using pragma Import. Also, have a look at descriptions of the Ada
package hierarchy "Interfaces".

The imported functions will have to match subprogram declarations
in your package mylibpkg.

The documentation that came with your compiler should explain
the switches needed for linking with shared libraries.



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

* Re: Shared library in Ada
  2008-04-19 11:41 ` Georg Bauhaus
@ 2008-04-19 11:51   ` Sebastien
  2008-04-19 15:31     ` Robert A Duff
  0 siblings, 1 reply; 9+ messages in thread
From: Sebastien @ 2008-04-19 11:51 UTC (permalink / raw)


> I suppose you are referring to dynamic linking at the OS level?

Yes I am.

> In this case you need to import functions from the library, perhaps
> using pragma Import. Also, have a look at descriptions of the Ada
> package hierarchy "Interfaces"

But it's for interfacing with other languages, isn't it?
The point is I don't want to interface with some other language, I want 
to create Shared object in Ada and then link with this shared object

> The imported functions will have to match subprogram declarations
> in your package mylibpkg.

So you mean that if I use the pragma export in my ads file, everything 
is going to be fine?

For instance:
function My_Ada_Function return Integer is begin
   return 1;
end My_Ada_Function;
pragma Export
(Convention    => C,
  Entity        => My_Ada_Function,
  External_Name => "My_Ada_Function" );

Or will I have to get two ads file? One with the pragma Export to create 
the shared object, and then one with the Pragma import to use the shared 
object in my program?

> The documentation that came with your compiler should explain
> the switches needed for linking with shared libraries.

Yes I think I can find out by myself especially because I'm using gnat ;-)

Sebastien




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

* Re: Shared library in Ada
  2008-04-19 11:27 Shared library in Ada Sebastien
  2008-04-19 11:41 ` Georg Bauhaus
@ 2008-04-19 11:52 ` Ludovic Brenta
  2008-04-19 11:55   ` Sebastien
  1 sibling, 1 reply; 9+ messages in thread
From: Ludovic Brenta @ 2008-04-19 11:52 UTC (permalink / raw)


Sebastien writes:
> Now there is only one things I didn't get information about.
>
> I'd like to create a share object in Ada and use it in an Ada software.

Since you mentioned .ads files I assume you use GNAT on GNU/Linux or
*BSD.  In that case your best source of information is the GNAT User's
Guide, chapter 19 "GNAT and Libraries":

http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gnat_ugn_unw/GNAT-and-Libraries.html

HTH

-- 
Ludovic Brenta.



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

* Re: Shared library in Ada
  2008-04-19 11:52 ` Ludovic Brenta
@ 2008-04-19 11:55   ` Sebastien
  2008-04-19 12:47     ` Sebastien
  0 siblings, 1 reply; 9+ messages in thread
From: Sebastien @ 2008-04-19 11:55 UTC (permalink / raw)


> Since you mentioned .ads files I assume you use GNAT on GNU/Linux or
> *BSD.  In that case your best source of information is the GNAT User's
> Guide, chapter 19 "GNAT and Libraries":
> 
> http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gnat_ugn_unw/GNAT-and-Libraries.html

Yes I'm running Gnat on FreeBSD.
Thanks about this link, I didn't know.

Sebastien




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

* Re: Shared library in Ada
  2008-04-19 11:55   ` Sebastien
@ 2008-04-19 12:47     ` Sebastien
  2008-04-19 13:39       ` Ludovic Brenta
  0 siblings, 1 reply; 9+ messages in thread
From: Sebastien @ 2008-04-19 12:47 UTC (permalink / raw)


> Yes I'm running Gnat on FreeBSD.
> Thanks about this link, I didn't know.

I follow the instruction and did the sample thing:

Lib:
::::::::::::::
lib/mylib.adb
::::::::::::::
with Ada.Text_IO; use Ada.Text_IO;

package body MyLib is
         procedure Print is
         begin
                 Put_Line("The lib");
         end Print;
end MyLib;
::::::::::::::
lib/mylib.ads
::::::::::::::
package MyLib is

         procedure Print;
         pragma Export (Convention => C, Entity => Print, External_Name 
=> "Print");
end MyLib;

::::::::::::::
main.adb
::::::::::::::
with MyLib; use MyLib;

procedure Main is
begin
         Print;
end Main;
::::::::::::::
mylib.ads
::::::::::::::
package MyLib is

         procedure Print;
         pragma Import (Convention => C, Entity => Print, External_Name 
=> "Print");
end MyLib;
::::::::::::::
main.gpr
::::::::::::::
Project Main is
         for Externally_Built use "true";
         for Source_Files use ("main.adb");
         pragma Linker_Options ("-lmylib");
end Main;

I compile the lib using:
gcc-4.1 -g3 -c mylib.adb
and link:
gcc-4.1 -shared -g3 -o ../libmylib.so mylib.o
I compile the main program using
gcc-4.1 -g3 -c mylib.ads
gcc-4.1 -g3 -c mylib.adb
and link:
gnatbind -x main.ali
gnatlink main.ali -lmylib

I got the following error:
# ./main

raised ADA.IO_EXCEPTIONS.STATUS_ERROR : s-fileio.adb:187

If I don't use shared object I got the program working, what am I missing?

Sebastien




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

* Re: Shared library in Ada
  2008-04-19 12:47     ` Sebastien
@ 2008-04-19 13:39       ` Ludovic Brenta
  2008-04-21 10:09         ` Sébastien
  0 siblings, 1 reply; 9+ messages in thread
From: Ludovic Brenta @ 2008-04-19 13:39 UTC (permalink / raw)


Sebastien <seb.morand@gmail.com> writes:
>> Yes I'm running Gnat on FreeBSD.
>> Thanks about this link, I didn't know.
>
> I follow the instruction and did the sample thing:

[snipped]

- Since both your library and your test program are in Ada, you don't
  need pragma Import or pragma Export.

- You do not need to duplicate the mylib.ads file.

- If you're going to use project files, you should go all the way and
  define a library project file for your library, and use that to
  build the library.

The library:

package My_Lib is
   pragma Preelaborate;
   procedure Print;
end My_Lib;

with Ada.Text_IO;
package body My_Lib is
   procedure Print is
   begin
      Ada.Text_IO.Put_Line ("Hello, World.");
   end Print;
end My_Lib;

project My_Lib is
   for Source_Dirs use ("my_lib");
   for Object_Dir use "my_lib.o";
   for Library_Name use "mylib";
   for Library_Kind use "dynamic";
   for Library_Dir use "lib";
   for Externally_Built use "false";
end My_Lib;

You then build the library like this:

gnatmake -Pmy_lib

This will produce ./lib/libmylib.so.

Then, the client:

with My_Lib;
procedure Main is
begin
   My_Lib.Print;
end Main;

with "my_lib";
project Main is
   for Source_Dirs use (".");
   for Main use ("main");
end Main;

You would build your client program like this:

gnatmake -Pmain

which will link main dynamically with lib/libmylib.so.

-- 
Ludovic Brenta.



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

* Re: Shared library in Ada
  2008-04-19 11:51   ` Sebastien
@ 2008-04-19 15:31     ` Robert A Duff
  0 siblings, 0 replies; 9+ messages in thread
From: Robert A Duff @ 2008-04-19 15:31 UTC (permalink / raw)


Sebastien <seb.morand@gmail.com> writes:

>> I suppose you are referring to dynamic linking at the OS level?
>
> Yes I am.
>
>> In this case you need to import functions from the library, perhaps
>> using pragma Import. Also, have a look at descriptions of the Ada
>> package hierarchy "Interfaces"
>
> But it's for interfacing with other languages, isn't it?

Yes, that's the main use.  But you are allowed to use Ada as a
convention, and this is occassionally useful.

However, I don't think you need any Export/Import pragmas to make shared
libraries.

- Bob



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

* Re: Shared library in Ada
  2008-04-19 13:39       ` Ludovic Brenta
@ 2008-04-21 10:09         ` Sébastien
  0 siblings, 0 replies; 9+ messages in thread
From: Sébastien @ 2008-04-21 10:09 UTC (permalink / raw)


Thanks for you help, it's working fine.

Sebastien

> Sebastien <seb.morand@gmail.com> writes:
>>> Yes I'm running Gnat on FreeBSD.
>>> Thanks about this link, I didn't know.
>> I follow the instruction and did the sample thing:
> 
> [snipped]
> 
> - Since both your library and your test program are in Ada, you don't
>   need pragma Import or pragma Export.
> 
> - You do not need to duplicate the mylib.ads file.
> 
> - If you're going to use project files, you should go all the way and
>   define a library project file for your library, and use that to
>   build the library.
> 
> The library:
> 
> package My_Lib is
>    pragma Preelaborate;
>    procedure Print;
> end My_Lib;
> 
> with Ada.Text_IO;
> package body My_Lib is
>    procedure Print is
>    begin
>       Ada.Text_IO.Put_Line ("Hello, World.");
>    end Print;
> end My_Lib;
> 
> project My_Lib is
>    for Source_Dirs use ("my_lib");
>    for Object_Dir use "my_lib.o";
>    for Library_Name use "mylib";
>    for Library_Kind use "dynamic";
>    for Library_Dir use "lib";
>    for Externally_Built use "false";
> end My_Lib;
> 
> You then build the library like this:
> 
> gnatmake -Pmy_lib
> 
> This will produce ./lib/libmylib.so.
> 
> Then, the client:
> 
> with My_Lib;
> procedure Main is
> begin
>    My_Lib.Print;
> end Main;
> 
> with "my_lib";
> project Main is
>    for Source_Dirs use (".");
>    for Main use ("main");
> end Main;
> 
> You would build your client program like this:
> 
> gnatmake -Pmain
> 
> which will link main dynamically with lib/libmylib.so.
> 

> Sebastien <seb.morand@gmail.com> writes:
>>> Yes I'm running Gnat on FreeBSD.
>>> Thanks about this link, I didn't know.
>> I follow the instruction and did the sample thing:
> 
> [snipped]
> 
> - Since both your library and your test program are in Ada, you don't
>   need pragma Import or pragma Export.
> 
> - You do not need to duplicate the mylib.ads file.
> 
> - If you're going to use project files, you should go all the way and
>   define a library project file for your library, and use that to
>   build the library.
> 
> The library:
> 
> package My_Lib is
>    pragma Preelaborate;
>    procedure Print;
> end My_Lib;
> 
> with Ada.Text_IO;
> package body My_Lib is
>    procedure Print is
>    begin
>       Ada.Text_IO.Put_Line ("Hello, World.");
>    end Print;
> end My_Lib;
> 
> project My_Lib is
>    for Source_Dirs use ("my_lib");
>    for Object_Dir use "my_lib.o";
>    for Library_Name use "mylib";
>    for Library_Kind use "dynamic";
>    for Library_Dir use "lib";
>    for Externally_Built use "false";
> end My_Lib;
> 
> You then build the library like this:
> 
> gnatmake -Pmy_lib
> 
> This will produce ./lib/libmylib.so.
> 
> Then, the client:
> 
> with My_Lib;
> procedure Main is
> begin
>    My_Lib.Print;
> end Main;
> 
> with "my_lib";
> project Main is
>    for Source_Dirs use (".");
>    for Main use ("main");
> end Main;
> 
> You would build your client program like this:
> 
> gnatmake -Pmain
> 
> which will link main dynamically with lib/libmylib.so.
> 



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

end of thread, other threads:[~2008-04-21 10:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-19 11:27 Shared library in Ada Sebastien
2008-04-19 11:41 ` Georg Bauhaus
2008-04-19 11:51   ` Sebastien
2008-04-19 15:31     ` Robert A Duff
2008-04-19 11:52 ` Ludovic Brenta
2008-04-19 11:55   ` Sebastien
2008-04-19 12:47     ` Sebastien
2008-04-19 13:39       ` Ludovic Brenta
2008-04-21 10:09         ` Sébastien

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