Re: .m to .mm causes missing function pointers
Re: .m to .mm causes missing function pointers
- Subject: Re: .m to .mm causes missing function pointers
- From: Chris Hanson <email@hidden>
- Date: Mon, 9 Jul 2007 07:43:18 -0700
On Jul 9, 2007, at 7:27 AM, David Farmer wrote:
I have added some ObjC sources ( using Cocoa frameworks ) to an
iTunes Visual plugin. Everything worked great until I decided to
convert the .m ( ObjC ) source into a .mm ( ObjC++ ) source. Now my
ObjC functions for that source file can no longer be linked. They
are missing. Just by renaming the source suffix to .mm.
C++ uses "name mangling" to implement function overloading. If a C++
(or Objective-C++) compiler sees a function call, it will generate a
call to a "mangled" version of the function's name that includes type
information. You need to mark the function as having C linkage using
the extern "C" construct when its declaration is seen by the compiler;
this will let the compiler know not to mangle its name.
The common way to do this is to either write the declarations in your
header files within an extern "C" block like this:
#ifdef __cplusplus
extern "C" {
#endif
void Function1(int foo);
long Function2(char *bar);
#ifdef __cplusplus
}
#endif
or to define your own MYAPP_EXTERN macro in some common header and
then use it like this:
/* common header */
#ifdef __cplusplus
#define MYAPP_EXTERN extern "C"
#else
#define MYAPP_EXTERN extern
#endif
/* other headers that include common header */
MYAPP_EXTERN void Function1(int foo);
MYAPP_EXTERN long Function2(char *bar);
You can see by inspecting some of their headers that the Cocoa
frameworks typically do the latter.
-- Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden