[GCC] "undefined reference" caused by Parameter order

Source: Internet
Author: User

I got in touch with GCC and makefile when I went to school, but I didn't find myself learning it until I started using it during my internship ......

Let's get down to the point. Today, we have a problem with compiling a series of "undefined reference". The command is as follows:

G ++-I/home/work/btestroot/COM/btest/gtest/output/include-L/home/work/btestroot/COM/btest/gtest/output/lib- l. -ltriple-lgtest_main triple_unittest.o-O triple_unittest <br/> 

Later, I found that the following command can be compiled successfully:

G ++-O triple_unittest triple_unittest.o-I/home/work/btestroot/COM/btest/gtest/output/include-L/home/work/btestroot/COM/btest/gtest/ output/lib-lgtest-lgtest_main-L. -ltriple

But I don't understand why. Baidu found the cause (see: http://hi.baidu.com/thinkingdeeply/blog/item/990035d18ecad0d0562c8468.html ):

For C/C ++ compilation, the read compilation option is executed from left to right (note 1 ). When the compiler encounters a source file, it starts to parse the functions used in the source file and find the corresponding function body or implementation (definition of function ). This process is performed in this order by first encountering unresolved functions, and then searching for possible function body information in some options following the source file options. Therefore, we can find that for makefile A, because the compilation option that contains the function body or function definition information appears before the source file, when the compiler encounters a function that cannot be parsed in the source file, find the relevant information in the option after the source file, and a compilation error occurs, that is, the related function definition cannot be found.

Therefore, the key difference between the above two commands is whether the "triple_unittest.o" is located before "-lgtest", and other things are on the cloud.

PS: It's really hard to catch a cold! Working with illness ~

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.