Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve link errors that appear in Objective-C++ but not Objective-C?

I'm converting my App Delegate file from .m to .mm (Objective-C to Objective-C++) so that I can access a third-party library written in Objective-C++. In Objective-C, my app delegate builds and runs fine. But when I change the extension, the project builds and I get link errors, all of which are missing symbols from a static library written in C that I use. The errors are classic link errors with the following format:

"MyFunction(arguments)", referenced from:

-[MyAppDelegate myMethod] in MyAppDelegate.o

Symbol(s) not found

All of the problems are in the app delegate object. I know that I'm all set to compile Objective-C++ because my ViewController file is .mm. So my question has a few parts to it.

First, are these symbols truly not there in the sense that I can't use them? In other words, is it not possible to access plain old C functions from an Objective-C++ file? If this is true, that's pretty unfortunate. I thought that almost all Objective-C code, and certainly all Objective-C code that at least builds as .mm, was valid Objective-C++. Am I wrong?

If not, any idea how I can prevent these errors? Are there header rules that are different in Objective-C++ that I don't know about?

Thanks for any and all help.

like image 264
Luke Avatar asked Dec 22 '22 00:12

Luke


1 Answers

Link errors with mixed C++/C or C++/Objective-C programs are usually due to C++ name mangling. Make sure you have extern "C" attached to all the appropriate declarations, and also that all of your code agrees on the linkage. That is, make sure that the function definition as well as the places where it is used can all see the extern "C" or extern "C++".

In your particular situation, it looks like MyFunction() is getting compiled with C++ linkage and having its name mangled, but that your myMethod Objective-C file is trying to link against the unmangled name.

Here is a link to the wikipedia article about name mangling.

like image 125
Carl Norum Avatar answered Apr 29 '23 16:04

Carl Norum