kerglen
kerglen

Reputation: 21

gnat gprbuild : how to build a dynamic dll and link with a static c++ library

I have a full Ada project I want to build to get a dynamic dll. Therefore I have to link it with another static library (myanotherlibrary.lib).

I use this command line :

gprbuild -d "D:\My_grp_project\My_grp_project.gpr"

Here the content of the .gpr :

project My_grp_project is

  Architecture := "x86";

  for Languages use ("Ada");
  for Source_Dirs use (".", "source", "source\common");
  for Library_Dir use "dll\" & Architecture;
  for Library_Ali_Dir use "ali\" & Architecture;
  for Library_Name use "My_grp_project";
  for Library_Kind use "dynamic";
  for Object_Dir use "obj\" & Architecture;

  package Linker is
   for Default_Switches ("Ada") use ("-L.", "-lbar");
  end Linker;

end My_grp_project;

I put "myanotherlibrary.lib" in the directory "D:\My_grp_project\", but it still doesn't link: "undefined reference to ..."

Could anyone help me please ?

Regards Glen

Upvotes: 1

Views: 2896

Answers (2)

kerglen
kerglen

Reputation: 21

Here the solution I finally found.

  1. It is not possible to link static library compiled with MSVC. I had to compile my static library with GCC (same version as the one included in GNAT).
  2. I had to add "Library_Options" options, without "-L" and "-l" arguments (another problem I passed). Note that package Linker is not taken into account while building a dynamic library. Note also that paths shall have no spaces !
project My_grp_project is

  for Languages use ("Ada");
  for Source_Dirs use (".", "source", "source\common");
  for Library_Dir use “dll";
  for Library_Ali_Dir use "ali";
  for Object_Dir use "obj";

  for Library_Name use "My_grp_project";
  for Library_Kind use "dynamic";
  for Library_Options use ("path\myanotherlibrary.a", "path_to_GNAT\libstdc++.a");

end My_grp_project;
  1. I builded the project in the GPS (default option) : "Build All"
  2. In result I do have my dynamic library "libMy_grp_project.dll"

Voilà. Thanks !

Upvotes: 1

Simon Wright
Simon Wright

Reputation: 25511

Looking at the docs, I think you should be using the Library_Options attribute instead of package Linker:

for Library_Options use ("-L.", "-lbar”);

(I’m confused - do you mean myanotherlibrary.lib or bar.lib?)

I’d be a bit concerned about using a static library from a dynamic library: I’d expect the dynamic library to be built with -fPIC or equivalent switch to get position-independent code, so that the same loaded library binary can be seen at different addresses in each of the executables using it.

Upvotes: 3

Related Questions