Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use both static and dynamically linked libraries in gcc

Tags:

gcc

linker

I need to distribute a binary that will run on as many x86 Linux distributions as possible. That means that I have to statically link some libraries, like glibc, because the user might not have the version I use. Other libraries have to be dynamically linked, like fontconfig, because it relies on a cache file format and hard coded locations that may differ on each system.

What are the command line options to do this? If I specify -static, then gcc will refuse to dynamically link any libraries at all.

like image 469
Steve Hanov Avatar asked May 01 '09 00:05

Steve Hanov


People also ask

Can you mix static and dynamic linking?

They don't mix Things get even worse if you try to mix static and dynamic linking: you can end up with multiple copies of a library in the same app. Say your app uses two libraries: FooKit and BarKit, and they both depend on a third, libCore.

Can you statically link a dynamic library?

You can't statically link a shared library (or dynamically link a static one).

What is the benefit of using a dynamic linked library DLL over a static linked library?

Dynamic linking has the following advantages over static linking: Multiple processes that load the same DLL at the same base address share a single copy of the DLL in physical memory. Doing this saves system memory and reduces swapping.

Is dynamic linking better than static?

1) The difference in runtime performance between static linking and dynamic linking is usually negligible. 2) (1) is not true if using a profiling compiler that uses profile data to optimize program hotpaths because with static linking, the compiler can optimize both your code and the library code.

How to link static library in C/C++ using gcc compiler?

How to Link Static Library in C/C++ using GCC compiler? Unlike Dynamic Link Library (DLL), the static library are pre-compiled and linked to the binary executables. Once built into the final executables, the static library cannot be shared among the others.

How does gcc/g++ choose which libraries to use?

The type of the libraries is chosen based on the qualifiers you provide. By default it will try to find dynamic (shared) libraries and if missing will attempt for static. Unless you tell it to use static libs only (-static). When you link to project libraries you tell the gcc/g++ which libraries to use in a manner (-lname).

What is the difference between static library and dynamic link library?

Unlike Dynamic Link Library (DLL), the static library are pre-compiled and linked to the binary executables. Once built into the final executables, the static library cannot be shared among the others. The static library is part of the binary code and that means it is loaded as the program starts i. e.

Is there dynamic querying of symbols in static libraries?

There will be no dynamic querying of symbols in static libraries. Many production line software use static libraries even today. Dynamic linking and Dynamic Libraries Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file.


1 Answers

Statically linking against any system library, and especially against libc, on modern UNIX or Linux systems makes the binary significantly less portable. Just don't do it.

Instead, use backward compatibility (binaries linked on an older system continue to run on all newer ones) to your advantage, either by linking your binary on an old system (I use RedHat 6.2, and I have not seen a Linux system where my binary will not run in the last 8 years), or by using something like autopackage (which has been deleted after this answer was written).

To answer your original question:

gcc main.o -Wl,-Bstatic -lfoo -Wl,-Bdynamic 

will cause linker to use archive version of libfoo. [It is important to have the trailing -Wl,-Bdynamic precisely so you don't force static libc.]

like image 63
Employed Russian Avatar answered Oct 04 '22 20:10

Employed Russian