The static library of the Linux/C + + link option--whole-archive,--no-whole-archive and--start-group,--end-group

Source: Internet
Author: User

Refer to these two blog posts:

Http://stackoverflow.com/questions/805555/ld-linker-question-the-whole-archive-option

http://codemacro.com/2014/09/15/inside-static-library/

These four are all linker options, so use-wl,[options] at compile time to pass to the linker, otherwise the compiler will not recognize this option.

The--whole-archive option solves problems that are commonly encountered in compilation. The linker does not add a symbol that is defined in the code, such as a function name, to the join table before it is used. As the following example:

A.cpp:

void func () {printf ("I AM in A.cpp.\n");}

Main.cpp:

extern void Func ();

int main () {func (); printf ("I AM in Main.cpp"); return 0;}

First compile g++-C a.cpp, then package Ar-r liba.a A.O.

If such a link g++-l-la Main.cpp-o Main, the linker will report an error, called Func () undefined. In fact, change the order can solve g++ main.cpp-l.-la-o Main.

or use g++-wl,--whole-archive-l.-LA-WL,--no-whole-archive Main.cpp-o Main, which links all the. O symbols in the liba.a.

For the register mechanism used in C + +, dynamic object creation encounters this problem. This problem is also encountered when using the Caffe library.

For--start-group and--end-group, GCC liner man is described as follows:

The archives should be a list of archive files. They may either explicit file names, or-l options.

The specified archives is searched repeatedly until no new undefined references is created. Normally, an archive was searched only once in the order that it was specified on the command line. If a symbol in this archive is needed to resolve an undefined symbol referred to by an object in an archive that appears L ater on the command line, the linker would is not being able to resolve that reference. By grouping the archives, they all is searched repeatedly until all possible references is resolved.

Using This option have a significant performance cost. It's best for use it if there is unavoidable circular references between, or more archives

There is a problem with the link order, the library in front of command line will depend on the library after, if the loop link is encountered,-lliba-llibb-lliba, This requires the use of--start-group and--end-group repeatedly in. A to search until all the undefined characters are found, rather than the default search only once. The MKL library has this problem.

The static library of the Linux/C + + link option--whole-archive,--no-whole-archive and--start-group,--end-group

Related Article

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.