How do I compile and link using gnatmake with an Ada shared library?

狂风中的少年 提交于 2019-12-05 15:18:49

OK, this turned out to be big pain but I found it.

The Florist binding is available both as a shared library and a static library (both are installed). You need to link against the library to resolve all references (I could never actually compile the library source into my application). Once you pass the argument to tell the compilation stage about the library you have to provide the .ads (at least) files to resolve against.

The final solution ended being this:

gnatmake -aI/usr/share/ada/adainclude/florist -aO/usr/lib/ada/adalib/florist demo.adb -largs -lflorist

-aI provides the path to the library's ads files. -aO provides the path to the (in this case) libflorist.so library file and finally (and this is the tricky one) you have to pass -lflorist to tell it what shared library this all is...but passing it won't work. You have to place a -largs in front of it (for the compile and linker) in order for the compilation phase to be passed the parameter! Without it those phases never see the argument!

So there you are everyone! In order to compile and link code against a shared Ada library under Linux (GCC) you need to provide the Library's Headers/Specs, the Library location, and the -llibname argument along with the -largs to pass those into the right places!

I'm happy now. Hope this helps someone else.

This is a simple example of using a GNAT Project to build with Florist. I assume that $ADA_PROJECT_PATH includes the directory where florist.gpr is installed (in my case, $HOME/local/lib/gnat).

The sample program (I couldn't find any simple Florist demos on the web, are there any?), in id.adb:

with POSIX.Process_Identification;
with Ada.Text_IO; use Ada.Text_IO;
procedure Id is
begin
   Put_Line (POSIX.To_String (POSIX.Process_Identification.Get_Login_Name));
end Id;

The project file (id.gpr), in the same directory as id.adb:

with "florist";
project Id is
   for Main use ("id.adb");
   for Object_Dir use ".build_id";
   for Exec_Dir use ".";
end Id;

Build with

$ gnatmake -p -P id.gpr
object directory "/Users/simon/florist-gpl-2010-src/demo/.build_id" created for project id
gcc -c -I- -gnatA /Users/simon/florist-gpl-2010-src/demo/id.adb
gnatbind -I- -x /Users/simon/florist-gpl-2010-src/demo/.build_id/id.ali
gnatlink /Users/simon/florist-gpl-2010-src/demo/.build_id/id.ali -lflorist -o /Users/simon/florist-gpl-2010-src/demo/id

and run:

$ ./id
simon

It looks like pretty garden-variety link errors. Something between your .h files, your Ada object files, and your C link libraries isn't quite mating up. Tough to tell what though.

Your best bet is going to be looking for other Florist users who may have run into the same issue. Their project page is on SourceForge here, but it looks like it hasn't been horribly active for the last 6 years. Since the latest version there is that old, it could be that you need a compiler (and OS?) that old for it to work with. Ick.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!