[Go] "Undefined reference to" problem solving method

Source: Internet
Author: User

Reprint could not find the original source.

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

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

About undefined reference Such problems, we are in fact often encountered, in this, I give a detailed example of the various reasons for common errors and solutions, I hope to help beginners.

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

The test code is as follows:

and then compile.

    1. Gcc-c test.c
    2. Gcc–c MAIN.C

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

      Gcc-o main MAIN.O

At this time, you will find that the error:

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

This is the most typical undefined reference error, because the implementation of a function cannot be found at link time, and in this case the TEST.O file contains the implementation of the test () function, so if you link it this way, it's okay.

    1. Gcc-o Main MAIN.O TEST.O

"Extension": in fact, above in order to let everyone more clear the bottom reasons, I have compiled links separate, the following compile will also report undefined reference wrong, in fact, the underlying cause is the same as above.

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

You need to change to the following form to succeed, compile the implementation file of the test () function together.

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

2. Missing associated library file (. a/.so) when linking

Here, just to give an example of a static library, assuming the source code is as follows.

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

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

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

    1. Gcc-c MAIN.C

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

    1. Gcc-o Main MAIN.O

You will find that the compiler has an error:

    1. /tmp/cccpa13l.o:in function ' main ':
    2. MAIN.C: (. text+0x7): Undefined reference to ' test '
    3. Collect2:ld returned 1 exit status

The root cause is also cannot find the test () function implementation file, because the implementation of the test () function in test.a this static library, so at the time of the link need to add test.a this library, link command modified to the following form.

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

"Extension": again, in order to clarify the problem, we separate the code compile link, if you want to build the executable program at once, you can execute the following command to MAIN.C and TEST.A.

    1. Gcc-o main main.c./TEST.A //Also, if you don't add test.a, you'll get an error .

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

This problem is more covert, but also I recently encountered with the online discussion of different issues, for example, first, or look at the test code.

As you can see, MAIN.C calls the TEST.C function, and the FUN.C function is called in test.c.
First, we compile the fun.c,test.c,main.c to generate the. o file.

    1. Gcc-c FUNC.C
    2. Gcc-c test.c
    3. Gcc-c MAIN.C

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

    1. AR–RC FUNC.A FUNC.O
    2. AR–RC test.a TEST.O

At this point, we are going to link main.o as an executable, because our MAIN.C contains a call to test (), so we should use TEST.A as our library file at the time of the link, the link command is as follows.

    1. Gcc-o Main MAIN.O test.a

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

    1. TEST.A (TEST.O): in function ' Test ':
    2. TEST.C: (. text+0x13): Undefined reference to ' func '
    3. Collect2:ld returned 1 exit status

That is, when we link, we find that our test.a called the Func () function and cannot find the corresponding implementation. From this we find that we also need to add the library file referenced by TEST.A in order to successfully link, so the command is as follows.

    1. Gcc-o main MAIN.O test.a FUNC.A

OK, so you can get the final program successfully. Similarly, if a third-party library (such as PTHREAD.A) is referenced in our library or program, it is also necessary to give the path and library file of the third-party library when linking, otherwise the undefined reference error will be obtained.

4 Multiple library file link order issues

This kind of problem is also very concealed, you may feel very inexplicable without careful study. We are still going back to the question discussed in section 3rd, and finally, if we change the order of the linked libraries, see what happens?

    1. Gcc-o main MAIN.O func.a test.a

We will get the following error.

    1. TEST.A (TEST.O): in function ' Test ':
    2. TEST.C: (. text+0x13): Undefined reference to ' func '
    3. Collect2:ld returned 1 exit status

Therefore, we need to note that in the link command to give the dependent library, we need to pay attention to the order of dependencies between libraries, the library depends on other libraries must be placed in front of the dependent library, so as to really avoid undefined reference errors, complete the compile link.

5. Linking C-language libraries in C + + code

If your library file is generated by C code, you will also encounter undefined reference problems when you link the functions in the library in C + + code. The following examples illustrate.

First, write the C language library file:

Compile, package as static library: TEST.A

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

So far, we got the Test.a file. Now let's start writing C + + files Main.cpp

Then compile main.cpp to generate the executable program:

    1. g++-o main main.cpp test.a

Will find an error:

    1. /tmp/ccjjicos.o:in function ' main ':
    2. Main.cpp: (. text+0x7): Undefined reference to ' test () '
    3. Collect2:ld returned 1 exit status

The reason is main.cpp for C + + code, called the C language library functions, so the link is not found, the solution: In the main.cpp, the C language library TEST.A related to the header file containing the addition of an extern "C" declaration can be. For example, the modified main.cpp is as follows:

    1. g++-o main main.cpp test.a

Re-compiling will find that the problem has been successfully resolved.

[Go] "Undefined reference to" problem solving method

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.