Using the GCC linker (static, dynamic linking): Difference between revisions

From Elvanör's Technical Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
This article explains how the gcc linker can be used to link libraries in an executable, either statically or dynamically.
This article explains how the gcc linker can be used to link libraries in an executable, either statically or dynamically.


== Command line options==
= Command line options =


Any option passed to the linker from gcc must use the syntax <tt>-Wl,<option></tt>. By default, gcc seems to link dynamically, eg against the .so files on Linux (.dylib on Mac OS X, .dll on Windows). To prevent dynamic linking, pass the <tt>-static</tt> option to the linker. It will then try to link statically, using the .a versions of the libraries.
Any option passed to the linker from gcc must use the syntax <tt>-Wl,<option></tt>. By default, gcc seems to link dynamically, eg against the .so files on Linux (.dylib on Mac OS X, .dll on Windows). To prevent dynamic linking, pass the <tt>-static</tt> option to the linker. It will then try to link statically, using the .a versions of the libraries.
Line 9: Line 9:
The <tt>-lexample</tt> option tells the linker to search for a library of the name ''libexample.a''.
The <tt>-lexample</tt> option tells the linker to search for a library of the name ''libexample.a''.


== Important information==
= Important information =


Even when you think you are linking statically, you may in fact have dependencies. This can happen when the .a libraries just reference the dynamic libraries. For example, Qt on Windows seem to built like that: even the .a libraries are present, they say to use the functions on the .dlls. You need to rebuild Qt statically in order to get .a libraries that contain the functions.
Even when you think you are linking statically, you may in fact have dependencies. This can happen when the .a libraries just reference the dynamic libraries. For example, Qt on Windows seem to built like that: even the .a libraries are present, they say to use the functions on the .dlls. You need to rebuild Qt statically in order to get .a libraries that contain the functions.
Line 15: Line 15:
In fact, even when linking dynamically, apparently the .a files are needed, since they list which functions are present in the dynamic versions.
In fact, even when linking dynamically, apparently the .a files are needed, since they list which functions are present in the dynamic versions.


== Useful Links ==
= Useful Links =


* [http://qtnode.net/wiki/Building_static Building a static Qt application]
* [http://qtnode.net/wiki/Building_static Building a static Qt application]

Latest revision as of 15:55, 8 June 2019

This article explains how the gcc linker can be used to link libraries in an executable, either statically or dynamically.

Command line options

Any option passed to the linker from gcc must use the syntax -Wl,<option>. By default, gcc seems to link dynamically, eg against the .so files on Linux (.dylib on Mac OS X, .dll on Windows). To prevent dynamic linking, pass the -static option to the linker. It will then try to link statically, using the .a versions of the libraries.

Note that in both cases, it will search for the libraries on some directories, usually specified by the Makefile (these directories appear on the command-line arguments given to the linker, they follow the -L flag). The name of the libraries themselves is given by the -l flag, for example -lmath.

The -lexample option tells the linker to search for a library of the name libexample.a.

Important information

Even when you think you are linking statically, you may in fact have dependencies. This can happen when the .a libraries just reference the dynamic libraries. For example, Qt on Windows seem to built like that: even the .a libraries are present, they say to use the functions on the .dlls. You need to rebuild Qt statically in order to get .a libraries that contain the functions.

In fact, even when linking dynamically, apparently the .a files are needed, since they list which functions are present in the dynamic versions.

Useful Links