[C/C ++] undefined reference

Source: Internet
Author: User

From: http://ticktick.blog.51cto.com/823160/431329


Recently, programming in Linux finds a strange phenomenon, that is, errors are always reported when a static library is linked, similar to the following errors:
(. Text + 0x13): Undefined reference to 'func'

You may often encounter problems such as undefined reference. Here, I will give a detailed example of the causes of Common Errors and solutions to help beginners.

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

The test code is as follows:

Then compile.
Gcc-C test. c
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. o

At this time, you will find that an error is reported:
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 file of a function cannot be found at the link time. In this example, test. the o file contains the implementation of the test () function, so it will be okay if you follow the method below.
Gcc-O main. O test. o

[Extension]: in fact, in order to make everyone better understand the underlying causes, I separated the compilation link. The following compilation will also report the undefined reference error. In fact, the underlying causes are the same as those above.
Gcc-O main. C // The test () implementation file is missing

You must change it to the following format to compile the implementation file of the test () function.
Gcc-O main. C test. C // OK, no problem

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

Here, we will only give an example of a static library, assuming the source code is as follows.

First, compile test. c into a static library (. a) file.
Gcc-C test. c
Ar-RC test. A test. o

Now we have the test. A file. Let's start compiling main. C.
Gcc-C main. c

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

You will find that the compiler reports 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 that the implementation file of the test () function cannot be found. in the static library a, you need to add test after the link. the link command for database a is changed to the following format.
Gcc-O main. O./test. A // Note:./is the path of test..

[Extension]: similarly, to clarify the problem, we separated the compilation link of the code above. If you want to generate an executable program at one time, you can. C and test. a. execute the following command.
Gcc-O main. C./test. A // Similarly, if test. A is not added, an error is returned.

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

This type of problem is relatively hidden, and it is also a different problem that I recently encountered from the discussion on the Internet. The example is as follows. First, let's take a look at the test code.

As you can see, 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.
Gcc-C func. c
Gcc-C test. c
Gcc-C main. c

Then, package test. C and func. c into static library files.
Ar-RC func. A func. o
Ar-RC test. A test. o

At this time, we are going. the olink can be an executable program, because our main. C contains a call to test (). Therefore, test. A is our library file. The link command is as follows.
Gcc-O main. O test.

At this time, the compiler will still report an error, as shown below:
Test. A (test. O): In function 'test ':
Test. c :(. Text + 0x13): Undefined reference to 'func'
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 make the connection successful. Therefore, the command is as follows.
Gcc-O main. O test. A func.

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

4. Order of file links in multiple databases

This kind of problem is also very concealed, and you may feel very inexplicable if you do not study it carefully. We still go back to the question discussed in section 3rd. At the end, if we change the order of the connected database, we can see what will happen?
Gcc-O main. O func. A test.

The following error is returned.
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 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, the undefined reference error can be avoided and the compilation link can be completed.

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

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

First, write the C language library file:

Compile and package it as a static library: test.
Gcc-C test. c
Ar-RC test. A test. o

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

Then compile main. cpp to generate the executable program:
G ++-O main. cpp test.

An error is reported:
/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 is C ++ code and calls the C language library function. Therefore, the link cannot be found. Solution: in main. in CPP, test. add an extern "C" declaration to the header file of File. For example, the modified main. cpp is as follows:

G ++-O main. cpp test.

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

6. Summary

Of course, the above several are the more common undefined reference I found the cause of the error and the solution, may also have other reasons, you are welcome to send a letter to the lujun.hust@gmail.com, to supplement this document, beginners solve various problems encountered during the learning process.

This article from the "three shadows" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/431329

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.