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

I am converting an App Delegate file from .m to .mm (Objective-C to Objective-C ++) in order to access a third-party library written in Objective-C ++. In Objective-C, my application delegate builds and works fine. But when I change the extension, the project builds, and I get link errors, all of which skip characters from the static library written in C that I use. Errors are classic link errors in the following format:

"MyFunction (arguments)" referenced:

- [MyAppDelegate myMethod] at MyAppDelegate.o

Symbol not found

All problems are present in the application delegation object. I know that I am configured to compile Objective-C ++ because my ViewController file is .mm. Therefore, my question consists of several parts.

Firstly, do these symbols really not exist in the sense that I cannot use them? In other words, is it impossible to access the plain old C functions from an Objective-C ++ file? If true, this is pretty unfortunate. I thought that almost all Objective-C code, and, of course, Objective-C code, which at least builds as .mm, was valid Objective-C ++. I am wrong?

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

Thanks for any help.

+4
source share
2 answers

Link errors with mixed C ++ / C or C ++ / Objective-C programs are usually associated with a C ++ name change. Make sure you have extern "C" attached to all relevant ads, and that all of your code agrees to the link. That is, make sure that the definition of the function, as well as the place where it is used, can be seen extern "C" or extern "C++" .

In your specific situation, it looks like MyFunction() compiled using C ++ linkage and has its name crippled, but your file myMethod Objective-C is trying to link with an unconnected name.

Here is a link to a wikipedia article on name mangling .

+8
source

The voluminous header includes extern C

This tells the linker that the function names in the library do not have a C ++ language name.

eg:.

  extern "C" { #include "my-lib.h" } 
+3
source

All Articles