Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking static fortran libraries with inter-dependencies

I am developing a Fortran program prog1 and two Fortran libraries, lib1 and lib2. prog1 depends on lib1 and lib2, and lib1 also depends on lib2. The directory structure might look like:

prog1
|- lib2
|- lib1
   |- lib2

In some cases, I would like prog1 to use a different version of lib2 than what lib1 uses. However, if the static library lib1.a includes the object files from lib2, the linker gives me errors like

./lib1/lib1.a(lib2_module.o): In function `__lib2_module_MOD_function1':
./lib2/src/lib2_module.f90:12: multiple definition of `__lib2_module_MOD_function1'
./lib2/lib2.a(lib2_module.o):./lib2/src/lib2_module.f90:12: first defined here

In other words, the linker is confused since both libraries contain the same object file lib2_module.o, but with different versions.

So my question is: How can two Fortran libraries contain (and use) different versions of the same object files?

like image 511
halvorlu Avatar asked Nov 02 '22 05:11

halvorlu


1 Answers

I do not think it is possible to do it by usage of static libraries. It is because from prog1 prespective, function1 from lib2's module is simple defined 2 times. On Wikipedia you can see that

...external functions and variables which are resolved in a caller at compile-time and copied into a target application...

And you would probably not be suprised that this code would generate similar errors you see in your question:

prog1.f90

some code here... 

function funtion1(x) ...
  ...first version of function1 ...
end function

function funtion1(x) ...
  ...second version of function1 ...
end function

some code here...

One possibility would be to compile at least lib1 as shared library and make sure (compiler/platform dependent) that the symbols from lib2 are not exported from shared lib1.

like image 103
Peter Avatar answered Nov 09 '22 12:11

Peter