Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29278 Discussions

Linking and function multiple definitions

Roberto_Sartori
Beginner
1,104 Views

Hi,

I'm working with Intel Fortran 10. I'll try to explain as easy as possible my problem. I've got three static library A,B and C. B and C uses some functions embedded in A. When I create an application which uses B and C I have to link A also.I don't want this, I want to embed the code of A directly inside B and C to make my application linking only B and C. I do this by setting "Link Library Dependencies" to YES when building B and C but when my application links B and C it finds multiple definitions of A.

Is possible to embed the code of A inside B and C without making the function of A being visible also from B and C ?

Hope to be clear...

Thanks,

roberto

0 Kudos
4 Replies
Steven_L_Intel1
Employee
1,104 Views

I think you may need to upgrade to version 11 to get this to work right. You've got the right idea - the "library dependences" option should merge library A with B or C when you build those. I am not sure why you'd get multiple definitions, though.

0 Kudos
Roberto_Sartori
Beginner
1,104 Views

I think you may need to upgrade to version 11 to get this to work right. You've got the right idea - the "library dependences" option should merge library A with B or C when you build those. I am not sure why you'd get multiple definitions, though.

Well, suppose the function fprintf() being in A and both B and C calls fprintf(). If I link B and C merged with A then the code of fprintf() is copied inside B and C but fprintf() becomes also "visible" outside B and C. So when I link B and C in the same project, the linker finds 2 definitions of fprintf(), one in library B and the other in library C. If I need to link also the library A the linker will find a third definition of fprintf().

In my library, I just want to merge the external functions without making this functions visible from my library too. Some kind of "private merging" of external functions.

No way with version 10 ?

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,104 Views
Quoting - s.robyx

If I link B and C merged with A then the code of fprintf() is copied inside B and C but fprintf() becomes also "visible" outside B and C.

Don't do it, then.

Seriously, a "normal" way to use static libraries is to maintain A separately from B and C, not to embed A within B and C. Yes, it's a bit inconvenient having to keep all dependent libraries around, but it's still more inconvenient to have multiple copies of the same code hanging around (especially if they come in different versions)...

In other words, do not include A into projects of B and C -- only in the target exe.

If you still want to do it the old way, you can try using /force:multiple linker flag. (I'm not sure it will always work, but you can give it a try).

0 Kudos
GVautier
New Contributor III
1,104 Views

If I am right, when a function or subroutine is linked from a static library all the functions and subroutine that where included in the same .obj file are linked at the same time.

Example

Library A

A123.for -> A123.obj source containing A1 A2 and A3 functions

Library B

Using A1 and A2 functions

Library C

Using A1 and A3 functions

In this case, if you create an .exe including B+A lib and C+A lib, you will have a multiple definition error for A1 function because A123.obj is included in each library.

So if you want to avoid multiple definition when including the static library A in static libraries B and C, you must build A with each source file includind only one function or subroutine or carrefully check the cross references of your functions and subroutines in each source file of A library.

I think that Yougoslav advice to not include A library in B and C libraies is the simpliest way.

0 Kudos
Reply