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
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
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
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
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