Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a C++ Static Library link to shared library?

Say I have a static C++ lib, static.lib and I want to call some functions from a C++ shared lib, say shared.lib. Is it possible?

Now assume that I have another shared lib, say shared2.lib which links to static.lib but does not link to shared.lib. Does the linker automatically link shared2.lib to shared.lib in this case?

I am using Microsoft Visual Studio 2003.

like image 778
Canopus Avatar asked Aug 07 '09 03:08

Canopus


People also ask

Can you link a static library to a dynamic library?

There is nothing like “linking a static library to dynamic library”. “Linking” is a process of completing the “symbol table” present in every executable or a shared library (see “Linkers and symbol tables”).

Can a static library depend on another static library?

Static Library Chains This is because static libraries don't link to other static libraries: All of the executable code only gets embedded in the executable. Technically, then, no static library depends on any other static library.

What happens when you link a static library?

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.


3 Answers

Static libraries are not linked. They are just a collection of object files (*.obj or *.o) that are archived together into a library file (kind of like a tar/zip file) to make it easier for the linker to find the symbols it needs.

A static lib can call functions that are not defined (but are only declared in a header file), as it is only compiled. Then when you link an exe or dll that uses the static lib you will have to link with another library that provides the called from the static lib but not defined in it.

If you want to the linker to automatically link other libraries Stephen's suggestion will work and is used by very reputable libraries like boost and stlport. To do this put the pragma in the main header file for the static library. You should include the static library and its dependants.

However IMO this feature is really meant for library writers, where the library is in the system library path so the linker will easily find it. Also in the case of boost and stlport they use this feature to support multiple version of the same libraries with options defined with #defines where different options require different versions of the library to be linked. This means that users are less likely to configure boost one way and link with a library configured another.

My preference for application code is to explicitly link the required parts.

like image 84
iain Avatar answered Sep 28 '22 06:09

iain


The linker will not automatically bring in the other libraries, but you can use #pragma comment (lib, "static.lib") to simplify the process of linking the additional files by adding the pragma to your header files.

like image 35
Stephen Nutt Avatar answered Sep 28 '22 07:09

Stephen Nutt


Say I have a static C++ lib, static.lib and I want to call some functions from a C++ shared lib, say shared.lib. Is it possible?

Yes for instance when you call windows functions from within your static lib they are normally from some dynamic library so there should be no difference.

Now assume that I have another shared lib, say shared2.lib which links to static.lib but does not link to shared.lib. Does the linker automatically link shared2.lib to shared.lib in this case?

Having dependencies like this one could cause problems later, I would suggest that you instead dynamically load the libraries using LoadLibrary(), that way you don't need to keep track of such dependencies while compiling/linking.

like image 24
AndersK Avatar answered Sep 28 '22 07:09

AndersK