A summary of the link issues under Linux (undefined reference)

Source: Internet
Author: User

Linux has been the compilation of links produced by the problem did not pay attention to, there are problems on the degree Niang, many times is indeed in search help to solve the bug, but because of the reason superficial understanding, no careful study, the results are always encountered in the bug when confused.

Even sometimes for a problem to check a half-day data, finally solved, but because no record or not clear the real reason, the result of the second encounter or to repeat the last toss, it is embarrassing helpless.

Although, the same error message, the cause of the various, but, summed up is good, so that do not know, as long as not the same thing repeated the same mistake, the more problems found, the more problems solved, the less the problem of unknown, is not it?

For Linux under the problem of compiling links, there are no shortage of people on the network of classic original Good article, have to say, now the development of the network let people: insufficient out of the family, you can do everything without asking for help. Cut the crap and talk about it:

1, let's look at one of the simplest examples, which is the most common "Hello world!" that we often get started with in programming languages Example:

<main.cpp>

#include <stdio.h>int main (intChar *agv[]) {    printf ("  Hello world\n");     return 0 ;}

The simplest way to compile this program is to g++ main.cpp, and then the compiler links the source file and finally generates the default a.out execution file. No error prompts occur during the period.

When we use the 3rd party software Library, we need to add some necessary compilation parameters (such as:-I library header file,-l library name,-l library Path) for the compiler to work without error prompts.

The reason is that: the former is a standard library, has been made into a system software library of a molecule, the compiler can be directly on the system to use, so from the surface will omit some parameters and details.

In fact, whether it is the use of the System standard library, or the use of 3rd-party software Generation Library, or our own software generation library, the compiler in the compilation of links, in addition to the library name and path is different, their compiler link principle is the same.

2, compiling and linking

Grammatical errors are the basis of comparison, we do not discuss;

"#include < header file name >" and "-i< directory >": A compilation problem is resolved, the former is the header file name, the latter is the directory where the header files are located

"-l< Library name >" and "-l< directory >": is a fixed link problem, the former is the library name, the latter is the directory where the name of the library

Here is an example of TEST.O, main->MAIN.O,

Each file source code is as follows:

Test.h

// test.h extern void test ();

Test.cpp

// test.cpp#include <stdio.h>
#include <test.h>void Test () { printf ("Hello world!\n" );}

Main.cpp

// main.cpp " test.h " int Main (void) {     test ();     return 0 ;}

Correct compilation steps:

[Email protected] link]# g++-o main main.cpp test.cpp

Now let's make a variant of this example:

Check the compile link parameter to see if there is any library file containing the interface.

2.1 Compile Option Missing library file (. o/.so/.a) at link time

[Email protected] link]# g++-o main main.cpp
main.o:in function ' main ':
Main.cpp: (. text+0x5): Undefined reference to ' test () '
Collect2:ld returned 1 exit status
[Email protected] link]# g++-o main test.cpp
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/. /.. /.. /.. /lib64/crt1.o:in function ' _start ':
(. text+0x20): Undefined reference to ' main '
Collect2:ld returned 1 exit status

1, in the Compile option, the library file (. o/.so/.a) is missing,
Example: g++ main test.cpp <main.cpp>
g++ main-l<lib1> -l<lib2>

2, the compiler option, multiple dependent libraries in the wrong order, especially for the static library, the order is very fastidious.
such as: main->test->func; so g++ main main.cpp func.a test.a will be reported

[Email protected] link]# g++-o main main.o func.a test.a
TEST.A (TEST.O): in function ' Test () ':
Test.cpp: (. text+0x5): Undefined reference to ' func () '
Collect2:ld returned 1 exit status

Attach the Static library generation command:

g++-C Test.cpp-o TEST.O
g++-C Func.cpp-o FUNC.O
Ar-rcs FUNC.A FUNC.O
Ar-rcs test.a TEST.O

2, the function interface is not implemented

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

a:test.h void Test ();
Test.cpp:void Test () {} or void Test (int n) {} or int test () {}

3, the function interface is implemented, but

3.1 Calling an interface named refactoring or an inconsistent calling convention causes an interface export mismatch, such as when a cross-language invocation interface (such as C + + calls to the interface of build library).
  

A summary of the link issues under Linux (undefined reference)

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.