& Quot; undefined reference to & quot; Solution

Source: Internet
Author: User
Programming in Linux recently finds a strange phenomenon, that is, when a static library is linked, errors are always reported, similar to the following:
 
 
  1. (.text+0x13): undefined reference to `func‘ 

We often encounter the undefined reference problem. Here, I will give a specific example to illustrate the causes of Common Errors and solutions, I hope it will help people who are new to learning.

1. the target file (. O) is missing during the link)

The following is the trial code:

 

Then compile.

 
 
  1. gcc -c test.c  
  2. gcc –c main.c 

Get two. O files, one is main. O and the other is test. O. Then we link. O to get the executable program:

 
 
    gcc -o main main.o 

At this time, you will find that an error is reported:

 
 
  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 file of a function cannot be found during the link, test. the o file contains the implementation of the test () function, so it is assumed that the link will be okay in the following way.

 
 
  1. gcc -o main main.o test.o 

[Extension]: in fact, in order to make everyone better understand the underlying reasons, I separated the compilation link. In this way, the undefined reference error will be reported during compilation. In fact, the underlying reasons are the same as those above.

 
 
  1. Gcc-O main. C // The test () implementation file is missing

The test () function must be compiled together with the implementation file.

 
 
  1. Gcc-O main. C test. C // OK, no problem

2. Related library files (. A/. So) are missing during the link)

Here, we will just give a static library example, if the source code is as follows.

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

 
 
  1. gcc -c test.c  
  2. ar -rc test.a test.o 

Now we have the test. A file. We started to compile main. C.

 
 
  1. gcc -c main.c 

At this time, the main. o file is generated, and then we can use the following command to link to get the executable program.

 
 
  1. gcc -o main main.o 

You will find that the compiler reports 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 that the implementation file of the test () function cannot be found, because the test () function is now tested. in the static library a, test must be added after the link. for the database A, the link command can be changed to the following form.

 
 
  1. Gcc-O main. O./test. A // Note:./is the path of test..

[Extension]: Same. In order to clarify the problem, we separated the compilation link of the code above. If we want to generate a runable program at a time, we can. C and test. a. Run the following command.

 
 
  1. Gcc-O main. C./test. A // is the same. If test. A is not added, an error is returned.

3. Another library file is used in the Linked Library file.

This problem is more concealed than the hidden one. It is also a different problem that I recently encountered with everyone on the Internet. For example, the following is an example. First, let's take a look at the testing code.

It can be seen that main. c calls the function test. C, and test. c calls the function fun. C.
First, compile fun. C, test. C, and Main. C to generate the. o file.

 
 
  1. gcc -c func.c  
  2. gcc -c test.c  
  3. gcc -c main.c 

Then, package test. C and func. c into static library files.

 
 
  1. ar –rc func.a func.o  
  2. ar –rc test.a test.o 

At this time, we are going. the olink can be used as a running program, because our main. c Includes the call to test (). Therefore, test. A serves as our library file. The link command is as follows.

 
 
  1. gcc -o main main.o test.a 

At this time, the compiler will still report an error, for example:

 
 
  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 to say, during the link, we found that test. A called the func () function and could not find the corresponding implementation. As a result, we found that we still need to add the library file referenced by test. A to the successful link. Therefore, the command is as follows.

 
 
  1. gcc -o main main.o test.a func.a 

OK. Similarly, assume that our library or program references a third-party library (such as pthread. a) the path and file of the third-party library must be provided at the same time as the link, otherwise the undefined reference error will be obtained.

4. Order of file links in multiple databases

This problem is also very concealed. If you do not study it carefully, you may feel inexplicable. We will go back to the question discussed in section 3rd. At the end, let's change the order of the connected database to see what will happen?

 
 
  1. gcc -o main main.o func.a test.a 

The following error is returned.

 
 
  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 pay attention to the dependency sequence between libraries when providing the dependent libraries in the link command. Libraries dependent on other libraries must be placed before the dependent libraries, in this way, we can truly avoid undefined reference errors and complete the compilation link.

5. link the C language library in the C ++ code

If your library file is generated by C code, the undefined reference problem will also occur when C ++ Code links functions in the library. The following is an example.

First, write the C language library file:

Compile and package it as a static library: test.

 
 
  1. gcc -c test.c  
  2. ar -rc test.a test.o 

Now we have the test. A file. Below we will start to compile the C ++ file main. cpp

Then compile main. cpp to generate the executable program:

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

An error is reported:

 
 
  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 is C ++ code and calls the C language library function. Therefore, the link cannot be found. Solution: in main. in CPP, test. a-related header file includes an extern "C" statement. For example, the modified main. cpp is as follows:

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

After compilation, you will find that the problem has been solved successfully.


& Quot; undefined reference to & quot; Solution

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.