Linking a static and dynamic library

In C ++, the static library A is linked to the dynamic libraries B and C. If the class A, Foo, is used in A, which is defined in B, will refer to C if it does not use Foo?

I thought the answer was yes, but now I run into a problem with xlc_r7 where the C library says that Foo is an undefined character that refers to C. My problem is that Library C does not use the class that references it. These are links in Win32 (VC6) and OpenVMS.

Is this a linker or PBCAK mismatch?

New information:

  • B depends on C, but not vice versa.

  • I do not use / OPT: REF for links to Windows and links without problems.

+2
source share
5 answers

When you statically link, the two modules become one. Therefore, when you compile C and the link A into it, as if you copied all of the source code A into the source code C, then you compiled the combined source. Thus, C.dll includes A, which has a dependency on B through Foo. You will need to link the C reference library to B in order to satisfy this dependency.

Please note that according to your information, this will create a cyclical relationship between B and C.

+4
source

This seems to be a linker (ld / unix), since (most of the versions I used) ld links libraries from left to right - and if the first one has a link that requires a later one - the usual trick is to add the first library (or any required library) at the end of the command.

Try and see ....

0
source

Is your link line for C, including the exporting lib for B? If so, then, as Richard says, it sounds like a thing to order.

Another suggestion is to see if there is a linker option to ignore non-reference characters if C does not need this function from A. For Microsoft linker, this is achieved using the / OPT: REF switch.

0
source

The only reason C is not referenced is because the compiler thinks it needs the Foo character.

Since C is not a Foo symbol, there must be another reason why the linker needs a symbol.

The only reason I know is because of some kind of export. I only know Visual C ++, so I suggest you find some equivalent __declspec( dllexport ) in the pre-processed files and see what generates it.

Here's what I would do: have the preprocessor output stored in a separate file and look for its occurrence Foo. Either this will happen as an export, or the compiler was somehow specified.

0
source

If a specific function definition is not required, then this library will not be bound during the binding phase. In your case, since the definition of foo is present in library B, and not in library C. Thus, library C will not be loaded into memory when loading an executable file.

But it looks like you are using the foo () function in the C library, because of which you are getting the corresponding error.

0
source

Source: https://habr.com/ru/post/1383945/


All Articles