t+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.
Gcc-o main MAIN.O test.a FUNC.A
OK, so you can successfully get the final program. Similarly, if we reference a Third-party library (such as PTHREAD.A) in our library or program, we also need to give the path and library file of the third party library when we link, otherwise we will get undefined reference error.
4 Multiple library file link order issues
The problem is also very subtle, and you may find it very confusing not to study it carefully. We are still going back to the issues discussed in section 3rd, and in the end, 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 St ATUs
Therefore, we need to note that in the link command to give the dependent libraries, we need to pay attention to the order of dependencies between the libraries, relying on other libraries must be placed in front of the dependent library, so as to really avoid undefined reference errors, complete the compilation link.
5. Link the C language library 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 version of the library file:
Compiling, packaging as a static library: TEST.A
Gcc-c test.c AR-RC test.a TEST.O
So far, we got the Test.a file. Here we start writing C + + file Main.cpp
Then compile the main.cpp to build 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, call the C language library function, so the link can not find the solution: that is, in Main.cpp, the C language Library TEST.A related header file contains a statement to add an extern "C". 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.
6. Summary
Of course, the above are some of the more common undefined reference errors and solutions that I have found, and there may be other reasons, please write lujun.hust@ gmail.com Communication, to supplement this document, the Novice to solve the learning process encountered in various problems.
Turn from: https://blog.csdn.net/csdn66_2016/article/details/70145962