• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE:Using C++ classes with Objective C code
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE:Using C++ classes with Objective C code


  • Subject: RE:Using C++ classes with Objective C code
  • From: Brant Sears <email@hidden>
  • Date: Fri, 10 Oct 2003 11:07:56 -0700

>At first it would not compile, but after cleaning the target, it
>compiled, but now it will not link.


This is a name mangling issue. C++ "mangles" the symbol so that it can
figure out which version of a particular method you meant. This is because
of the function overloading feature of C++ (which Obj-C doesn't have.)

So, when you are in a .mm or .cpp file and you include your .h file with
function prototypes, it assumes you mean the mangled symbols. When you
include the same header from your .m files and .c files, it assumes you mean
a non-mangled symbol.

The solution is to declare your interface to be non-mangled using extern
"C".

So, your file "interface.h" would look like this:

#ifdef __cplusplus
extern "C"
{
#endif

void foo(char* , int, UInt32 *);

void bar(char*, int *);

#ifdef __cplusplus
}
#endif

Remember that your .m and .c files won't even understand (and don't need)
extern "C", so that's why you have to use the preprocessor. __cplusplus will
be defined when you are including from a .cpp or .mm file.

Brant Sears
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Embedding frameworks in a palette
  • Next by Date: Re: Some small utility for Mac OS X.
  • Previous by thread: Re: Using C++ classes with Objective C code
  • Next by thread: Installer problems
  • Index(es):
    • Date
    • Thread