I'm getting this Apple Mach-O linker error:
Undefined symbols for architecture arm64:
"_TestFunction", referenced from:
-[ViewController viewDidLoad] in ViewController.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here's all the steps necessary to reproduce the error:
void TestFunction();
" to my .hvoid TestFunction(){}
" to my .mmTestClass.h
into my ViewController.m
and try and call TestFunction()
from my viewDidLoad.I'm doing some Core-Audio work and need to make some C++ calls in my MIDI callbacks, so I can't make any ObjectiveC method calls as they'd block the audio thread.
Maybe Xcode view TestFunction() as a C++ call, so won't make it from a pure ObjectiveC class? Is there any way of telling Xcode that it's a C function?
You need to mark your c++ function as extern "C"
.
After which objective c code can link to that function.
Your TestClass.h should look like this
#if defined __cplusplus
extern "C" {
#endif
void TestFunction();
#if defined __cplusplus
};
#endif
extern "C" makes a function-name in C++ have 'C' linkage (compiler does not mangle the name) so that client C code can link to (i.e use) your function using a 'C' compatible header file that contains just the declaration of your function. Your function definition is contained in a binary format (that was compiled by your C++ compiler) that the client 'C' linker will then link to using the 'C' name.
Since C++ has overloading of function names and C does not, the C++ compiler cannot just use the function name as a unique id to link to, so it mangles the name by adding information about the arguments. A C compiler does not need to mangle the name since you can not overload function names in C. When you state that a function has extern "C" linkage in C++, the C++ compiler does not add argument/parameter type information to the name used for linkage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With