Undefined symbols for architecture x86_64: ld: symbol(s) not found for architecture x86_64
If you are getting similar error in your code (while mixing C and C++ code)
Undefined symbols for architecture x86_64:
"func()", referenced from:
_main in cc_code-NhxeQf.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
or something like this, during linking phase
error: ‘your_symbol_goes_here’ was not declared in this scope
It might mean that you are trying to include C code in an inappropriate way.
Take a look at samples below
/* This is c_code.h */ void func();
/*This is c_code.c */
void func() {
int a = 1;
}
/* This is cc_code.cc */
#include "c_code.h"
int main() {
func();
return 0;
}
Now, let’s try to compile it:
shell> cc -o cc_code cc_code.cc c_code.c
Undefined symbols for architecture x86_64:
"func()", referenced from:
_main in cc_code-seLpaq.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 solution here is to mark explicitly, that symbols included in cc_code.cc are coming from C
/* This is modified cc_code.cc, now it is called cc_code_modified.cc*/
extern "C" {
#include "c_code.h"
}
int main() {
func();
return 0;
}
And now, there are no issues with compilation
shell> cc -o cc_code cc_code_modified.cc c_code.c
Make sure to check this page: http://www.parashift.com/c++-faq/index.html