gcc/g++ Link order for dynamic link libraries and static libraries

Source: Internet
Author: User

Transferred from: http://withc8212.blog.163.com/blog/static/11656983820109263562854/

So file: Dynamic library
A file: Static library
EXE file: Executable program (Linux under File attributes to indicate whether it is an EXE file, regardless of the suffix name)

After some of their own written test procedures, a general understanding of the next GCC link order problem, summed up the following points:
1, the dynamic library can contain another static library, through the parameter-lxxx the static library libxxx.a into so file, so that the file
All implementations of LIBXXX.A are included. Of course, if there is no problem with libxxx.a, the resulting so will be smaller.
If you do not include LIBXXX.A, you end up using this so executable file, which must be added to-lxxx when it is built.
2, 2 So files can contain the same static library LIBXXX.A, the final generation of EXE files, there will be no conflict. More broadly speaking, generating
EXE file, you can link multiple so files and a file, if there are multiple implementations of a file, eventually only one copy will take effect, and the other
will be ignored. Don't worry about conflict.
3, when the EXE is generated, when a file has multiple implementations, the leftmost specified a file takes effect.

For example:
LIBSTATIC.A: a static library file
Libdynamic1.so: You need to use a function in LIBSTATIC.A, but it does not contain LIBSTATIC.A
Libdynamic2.so: You need to use the functions in LIBSTATIC.A, including LIBSTATIC.A
Libdynamic3.so: You need to use the functions in LIBSTATIC.A and also include LIBSTATIC.A
Test.exe: Final generated executable (Linux suffix not required, in order to illustrate the file, let exe suffix to represent the executable file)

Gcc-o test.exe-ldynamic1: Error, libdynamic1.so does not contain LIBSTATIC.A, the implementation of LIBSTATIC.A could not be found.

Gcc-o test.exe-ldynamic1-lstatic: Correct, not in so, but specified LIBSTATIC.A, can be compiled

Gcc-o Test.exe-ldynamic2: Right, libdynamic2.so has LIBSTATIC.A implementation.

Gcc-o Test.exe-ldynamic1-ldynamic2: Right, there is no libstatic.a in libdynamic1.so, but there are in libdynamic2.so.

Gcc-o TEST.EXE-LDYNAMIC2-LDYNAMIC3: Correct, although libdynamic2.so and libdynamic3.so all contain static libraries, but do not conflict,
Eventually there will only be one, and the static library in libdynamic2.so is valid.

Gcc-o test.exe-ldynamic2-ldynamic3-lstatic: Equally true, eventually there will only be one, and the static library in libdynamic2.so is valid.

The link order of the libraries in GCC is from right to left, so put the library of the most basic implementation at the end so that Lib on the left can call the code in Lib on the right. At the same time, when the implementation code of a function exists in more than one Lib, the leftmost Lib code finally link, so it will eventually be saved.

gcc/g++ Link order for dynamic link libraries and static libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.