Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined symbols for architecture arm64. Make C calls into ObjC++ file?

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:

  1. I created a new single view universal Objective C project (Xcode Version 6.0.1 (6A317))).
  2. I then created a new ObjectiveC file called TestClass
  3. Change TestClass.m to TestClass.mm
  4. Add "void TestFunction();" to my .h
  5. Add "void TestFunction(){}" to my .mm
  6. Import TestClass.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?

like image 993
user3306249 Avatar asked Oct 16 '14 12:10

user3306249


1 Answers

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.

like image 91
l0gg3r Avatar answered Nov 15 '22 04:11

l0gg3r