"Undefined reference to" problem summary and solution

Source: Internet
Author: User

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

First write 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 command, we will get two. o files.

$ gcc-c test.c  

Subsequently, 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 Vocation)

Compile times is wrong, this is the most typical undefined reference error, because the implementation file for a function cannot be found at link time. If the link is correct as in the following way.

$ gcc-o Main MAIN.O TEST.O

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

$ gcc-o Main MAIN.C test.c
missing associated library file when linking

We compiled the test.c from 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 See invocation)

The root cause is also unable to find the implementation of the test () function file, because the test () function implementation in TEST.A this static library, so at the time of the link need to add test.a this 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 made the mistake ...). )

First change the code used in the first example, call the other functions in test (), and change the code as shown below.

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 compile the FUN.C and test.c first and generate the. o file.

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

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

$ AR–RC FUNC.A FUNC.O  

At this point, MAIN.C is compiled into an executable program, because MAIN.C contains a call to test (), so you should link the test.a as our library file, the link command is 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 for architecture x86_64
clang:error:linker command failed with exit code 1 (U Se-v to see invocation)

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

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

Similarly, if a third-party library (such as PTHREAD.A) is referenced in our library or program, it is necessary to give the path and library file of the third-party library at the time of the link, otherwise the undefined reference error will be obtained. Multiple library file link order issues

This kind of problem is very concealed, not careful study, may feel very inexplicable. 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 library, you need to pay attention to the order of dependencies between the library, the library depends 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: You can compile the pass correctly on your Mac. 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 <stdio.h> void Test (int c) {printf ("Just Test it\n");

    }//Main.c #include "test.h" int main (int argc, char **argv) {test (5);
return 0; }
The

First compiles the test.c into a library file.

$ gcc-c test.c $ ar-rc test.a test.o 

Compiles main.c into an executable file.

$ gcc-o main main.c test.a ld:warning:ignoring file test.a, file was built for archive which are not the architecture B Eing linked (x86_64): test.a Undefined symbols for architecture x86_64: "_test", referenced from: _main in Main-f2 7CF1.O Ld:symbol (s) not Foun 

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.