[Original] exploring the GCC Link Library sequence from a link Error

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://blog.csdn.net/clever101

 

Compile a small object using codeblocks10.05ProgramThe PNG Library and zlib library are used. I found that when compiling a PNG static library, I only need to specify the header file path of the zlib library, but do not need to link to the zlib library (this is a bit strange ). Then an error occurs when compiling a self-written Program:

Undefined reference to 'inflateinit _ '|

Inflateinit is a function in the zlib library. The compilation error is inflateinit _ (I guess it is because GCC adds modifiers to the function ). I checked that my header file paths and Lib files are all set. Why is this error? Later, I accidentally thought about whether it was the order of the connected database. I changed the following order:

To:

That is to say, if the PNG Library is mentioned before the zlib library and then the compilation error is re-compiled, it will disappear. Why?


I went to the Forum to ask for advice. The mlee79 hero told me that the GCC was in each symbol from the past to the next. If it could not be found, an error would be reported and it would not be found in the previous step. If you do not want to arrange the link sequence, add-xlinker "-(" $ (libs)-xlinker "-)" in the compilation option "-)". A simple example is:

 
$ For file in app. c f1.c f2.c f3.c; do echo ---- $ file; CAT $ file; done; gcc-C f1.c f2.c f3.c; \ ar CRF lib1.a f1.o f3.o; Ar CRF lib2.a f2.o; GCC app. c lib1.a lib2.a; \ GCC app. c-xlinker "-(" lib1.a lib2.a-xlinker "-)" ---- app. CINT main () {extern int F1 (INT, INT); extern int F2 (INT, INT); Return F1 (1, 2) + F2 (3, 4 );} ---- f1.cint F1 (int A, int B) {return a + B;} ---- f2.cextern int F3 (INT, INT); int F2 (int A, int B) {return a * B-F3 (a, B) ;}---- f3.cint F3 (int A, int B) {return a-B ;}lib2.a (f2.o): f2.c :(. text + 0x1e): Undefined reference to '_ F3' collect2: LD returned 1 exit status

Here is a simple example: the first lib1 (F3 symbol in lib1) linked by the program, when the program is linked to lib2, F3 is naturally not found (because lib1 is in front of lib2 ),

 

However, as you can imagine, adding-xlinker is certainly slow, because every symbol needs to search for all the libraries. Therefore, to solve such link errors, you still need to arrange the order of the connected database. The principle is that the upper-layer calling database is placed before the underlying called database. Thanks to mlee79!

 

Author: clever101 posted on 20:50:45 Original article link Read: 767 comments: 2 Views comments

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.