You need to add extern "C"
to the function implementation as well as the declaration. If the implementation of the function is in a .mm
(Objective-C++) file, it will be compiled as Objective-C++, with name mangling. It doesn't matter that you happen to have an extern "C"
declaration for a function with the same name. They are still seen as two different things.
So wrap your implementation in extern "C"
as well to remove the name mangling for the function implementation:
extern "C" void display(NSString *text) {
}
Or:
extern "C" {
void display(NSString *text) {
}
}
Also, if you want your header to be usable by C code, you will need to add an #ifdef
to only use extern
when compiling as C++. extern
is not a valid keyword in C:
#ifdef __cplusplus
extern "C" {
#endif
void display(NSString *text);
#ifdef __cplusplus
}
#endif
See Combining C++ and C - how does #ifdef __cplusplus work? for more information.