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:
- (. 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.
- Gcc-c test.c
- 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:
- 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 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.
- 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.
- 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.
- 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
- Gcc-c test.c
- AR-RC test.a TEST.O
So far, we got the Test.a file. We started compiling MAIN.C
- 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.
- 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 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.
- 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.
- 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.
- Gcc-c FUNC.C
- Gcc-c test.c
- Gcc-c MAIN.C
Then, TEST.C and FUNC.C are packaged into static library files respectively.
- AR–RC FUNC.A FUNC.O
- 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.
- Gcc-o Main MAIN.O test.a
At this point, the compiler will still error, as follows:
- TEST.A (TEST.O): in function ' Test ':
- TEST.C: (. text+0x13): Undefined reference to ' func '
- 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.
- 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?
- Gcc-o main MAIN.O func.a test.a
We will get the following error.
- TEST.A (TEST.O): in function ' Test ':
- TEST.C: (. text+0x13): Undefined reference to ' func '
- 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
- Gcc-c test.c
- 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:
- g++-o main main.cpp test.a
Will find an error:
- /tmp/ccjjicos.o:in function ' main ':
- Main.cpp: (. text+0x7): Undefined reference to ' test () '
- 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:
- 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