Summary of undefined reference problems

Source: Internet
Author: User

Recently in Linux programming found a strange phenomenon, is linked to a static library always error, similar to the following errors:

(. text+0x13): Undefined reference to ' func '

About undefined reference Such a problem, we actually often encounter, here, I give a detailed example of the various reasons for common mistakes and solutions, I hope to help beginners.

1. The link is missing the relevant target file (. o)

The test code is as follows:

Then compile.

Gcc-c test.c gcc–c main.c

Get two. o files, one is MAIN.O, one is TEST.O, then we link. O Get executable program:

Gcc-o Main MAIN.O

At this point, you will find that the error:

main.o:in function ' main ': main.c: (. text+0x7): Undefined reference to ' test ' Collect2:ld returned 1 exit status

This is the most typical undefined reference error, because discovering the implementation file for a function is not found in the link, in this case the TEST.O file contains the implementation of the test () function, so it's okay to link to the following way.

Gcc-o Main MAIN.O TEST.O

"Extension": in fact, above in order to let everyone more clear the underlying reason, I will compile the link separate, the following so compile also report undefined reference wrong, in fact the underlying reason is the same as above.

Gcc-o main MAIN.C//missing implementation file for test ()

You need to change to the following form to be successful and compile the implementation file of the test () function.

Gcc-o main MAIN.C test.c//ok, no problem.

2. Missing associated library file (. a/.so) at link time

Here, just to give a static library of examples, assume the source code as follows.

First compile the test.c into a static library (. A) file

Gcc-c test.c AR-RC test.a TEST.O

So far, we got the Test.a file. We start compiling MAIN.C

Gcc-c MAIN.C

At this point, the MAIN.O file is generated, and we then link to the following command to expect an executable program.

Gcc-o Main MAIN.O

You will find that the compiler has an error:

/tmp/cccpa13l.o:in function ' main ': main.c: (. text+0x7): Undefined reference to ' test ' Collect2:ld returned 1 exit St ATUs

The root cause is also the implementation of the test () function cannot be found, because the test () function is implemented in TEST.A this static library, so the link in the need to add test.a after the library, the link command modified to the following form.

Gcc-o main MAIN.O./TEST.A//NOTE:./is given the TEST.A path

"Extension": again, to make the problem clear, above we separate the compiled links for the code, and if you want to generate executable programs at once, you can execute the following commands for MAIN.C and TEST.A.

Gcc-o main main.c./TEST.A//Also, if not add TEST.A will also error

3. Another library file is used in the linked library file

This kind of question is quite covert, also is I recently encountered with the network everybody discusses the different question, the example explains as follows, first, or looks at the test code.

As can be seen from the diagram above, main.c called the test.c function, and the FUN.C function was called in test.c.
First, we compile the fun.c,test.c,main.c to generate an. o file.

Gcc-c func.c gcc-c test.c gcc-c main.c

Then, TEST.C and FUNC.C are packaged into static library files.

AR–RC func.a func.o AR–RC test.a TEST.O

At this point, we are going to link the MAIN.O as an executable program, because our MAIN.C contains a call to test (), so we should test.a as our library file when we link, and the link commands are as follows.

Gcc-o Main MAIN.O test.a

At this point, the compiler will still complain, as follows:

TEST.A (TEST.O): in function ' Test ': test.c: (. text+0x13): Undefined reference to ' func ' Collect2:ld returned 1 exit St ATUs

That is, when the link, we found that our test.a called the Func () function, can not find the corresponding implementation. As a result, we found that we also need to add the TEST.A reference to the library file in order to successfully link, so the order is as follows.

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.