Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static linking only some libraries

How can I statically link only a some specific libraries to my binary when linking with GCC?

gcc ... -static ... tries to statically link all the linked libraries, but I haven't got the static version of some of them (eg: libX11).

like image 213
peoro Avatar asked Nov 11 '10 15:11

peoro


People also ask

Are static libraries linked?

A static library is a programming concept in which shared libraries with special functionalities, classes or resources are linked to external applications or components, facilitating the creation of stand-alone and executable files.

What is the disadvantage of static linking in OS?

The major disadvantages of static linking are increases in the memory required to run an executable, network bandwidth to transfer it, and disk space to store it.

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.


2 Answers

gcc -lsome_dynamic_lib code.c some_static_lib.a

like image 195
Šimon Tóth Avatar answered Oct 13 '22 19:10

Šimon Tóth


You could also use ld option -Bdynamic

gcc <objectfiles> -static -lstatic1 -lstatic2 -Wl,-Bdynamic -ldynamic1 -ldynamic2 

All libraries after it (including system ones linked by gcc automatically) will be linked dynamically.

like image 41
Dmitry Yudakov Avatar answered Oct 13 '22 21:10

Dmitry Yudakov