Summary and solution of "undefined reference to" problem

Source: Internet
Author: User

Reprint Address: Https://segmentfault.com/a/1190000006049907?utm_source=tuicool&utm_medium=referral


In the actual process of compiling code, we often encounter the "undefined reference to" problem, simple can be easily solved, but some are hidden very deep, it takes a lot of time to check. Work encountered a variety of similar problems, according to the following several possible conditions to check, can help to clarify the clues, so as to quickly solve the problem. missing related target file on link

Start by writing the following test code:

Test.h

#ifndef __test_h__
#define __test_h__

void TEST ();

#endif

//test.c

#include <string.h>
#include <stdio.h>



void Test ()
{
    printf ( "Just test it\n");
}

Main.c

#include "test.h"

int main (int argc, char **argv)
{
    test ();

    return 0;
}

With the following commands, we will get two. o files.

$ gcc-c test.c  

Later, we will main.o this file and compile it into an executable file.

$ gcc-o Main MAIN.O
Undefined symbols for architecture x86_64:
  "_test", referenced from:
      _main in MAIN.O
Ld:symbol (s) not found for architecture x86_64
clang:error:linker command failed with exit code 1 (use-v to Vocation)

Compile times is wrong, this is the most typical undefined reference error, because the implementation file for a function was found in the link. The link is correct if you click this way.

$ gcc-o Main MAIN.O TEST.O

Of course, you can also follow the following command to compile, so that you can step into place.

$ gcc-o Main MAIN.C test.c
missing associated library file at link time

We compile the test.c in the first example into a static library.

$ gcc-c test.c  

Then compile the executable file, using the following command:

$ gcc-o Main MAIN.C 
Undefined symbols for architecture x86_64:
  "_test", referenced from:
      _main in main-6ac26d  . o
Ld:symbol (s) not found for architecture x86_64
clang:error:linker command failed with exit code 1 (use-v to Invocation)

The root cause is also the implementation of the test () function can not find the file, because the test () function of the implementation of the TEST.A in this static library, so the link in the need to join the TEST.A after the library, the link command modified to the following form.

$ gcc-o Main MAIN.C test.a

Another library file is used in the linked library file (This example is very, very good, I just made the mistake ...). )

To change the code used in the first example, call the other function in test (), and the changed code looks like this.

Func.h

#ifndef __func_h__
#define __func_h__

void func ();

#endif

//func.c

#include <stdio.h>

void func ()
{
    printf ("Call it\n");
}

Test.h

#ifndef __test_h__
#define __test_h__

void TEST ();

#endif

//test.c

#include <string.h>
#include <stdio.h>

#include "func.h"



void Test ()
{
    printf ("Just Test it\n");

    Func ();
}

Main.c

#include "test.h"

int main (int argc, char **argv)
{
    test ();

    return 0;
}

We first compile the FUN.C and test.c to generate an. o file.

$ gcc-c func.c  
$ gcc-c test.c

Then, TEST.C and FUNC.C are packaged into static library files.

$ AR–RC FUNC.A FUNC.O  

The MAIN.C is compiled into an executable program, and since MAIN.C contains a call to test (), it should be test.a as our library file at the time of the link, and the link commands are as follows.

$ gcc-o Main MAIN.C test.a
Undefined symbols for architecture x86_64:
  "_func", referenced from:
      _test in Test . A (TEST.O)
Ld:symbol (s) not found to architecture x86_64
clang:error:linker command failed with exit code 1 (U Se-v to invocation)

That is, when the link found test.a called the Func () function, can not find the corresponding implementation, we also need to test.a referenced to the library file also added in order to successfully link, so the command is as follows.

$ gcc-o main MAIN.C test.a FUNC.A

Similarly, if we reference a Third-party library (such as PTHREAD.A) in our library or program, we need to give the path and library file of the third party library in the link, otherwise we will get undefined reference error. Multiple library file link order problem

This kind of question is very covert, does not study carefully, may feel very inexplicably. In the third example, for the test code, change the order of the link libraries, as follows:

$ gcc-o Main main.c func.a test.a
test.a (TEST.O): in function ' Test ':  
test.c: (. text+0x13): undefined reference to ' Func '  
Collect2:ld returned 1 exit status

Therefore, in the link command to give the dependent libraries, you 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.

Note: The Mac can be compiled correctly. definition inconsistent with implementation

Write the test code as follows:

//test.h #ifndef __test_h__ #define __TEST_H__ void Test (unsigned int c); #endif//test.c #include <string.h> #include 

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.