Undefined symbols for arm64 architecture. Make C calls to ObjC ++ file?
I am 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 are all the steps required to reproduce the error:
- I created a new generic Objective C single view object (Xcode Version 6.0.1 (6A317))).
- Then I created a new ObjectiveC file called TestClass
- Change TestClass.m to TestClass.mm
- Add "
void TestFunction();
" to my .h - Add "
void TestFunction(){}
" to my .mm - Import
TestClass.h
to mineViewController.m
and try and callTestFunction()
from my viewDidLoad.
I am doing some Core-Audio work and have to make some C ++ calls in my MIDI callbacks, so I cannot make ObjectiveC method calls as they would block the audio stream.
Maybe Xcode view TestFunction () as a C ++ call so won't be made from a pure ObjectiveC class? Is there a way to tell Xcode that this is a C function?
source to share
You need to mark your C ++ function as extern "C"
.
Then the object code c can refer to this function.
Your TestClass.h should look like this:
#if defined __cplusplus
extern "C" {
#endif
void TestFunction();
#if defined __cplusplus
};
#endif
extern "C" makes the function name in C ++ have a reference to "C" (the compiler does not change the name) so that the C client code can refer to (ie use) your function using a compatible C compatible header file that contains only your function declaration. Your function definition is contained in a binary format (which has been compiled by your C ++ compiler) that the client-side C linker will refer to the name "C".
Since C ++ has function name overloading and C does not, the C ++ compiler cannot simply use the function name as a unique identifier for the reference, so it manages the name by adding information about the arguments. The AC compiler does not need to specify a name, as you cannot overload function names in C. When you specify that a function has an extern "C" reference in C ++, the C ++ compiler does not add parameter / parameter parameter information to the name. used for communication.
source to share