Re: Problem with Objective-C++ Header Files

Christopher Stern <[email protected]>
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
On Jan 8, 2010, at 10:23 AM, Stephen Furlani wrote:

> Hello,
> 
> I have attempted searching the archives, but the search function keeps
> truncating "objective-c++" to "objective-c  " so if there's a good location
> to look for this information, I can't find it.
> 
> I have an extensive library of C++ code that I want to be able to
> incorporate into my Objective-C project.  Following the tutorials provided
> by Apple and others, I've renamed my object file to .mm to utilize
> Objective-C++.
> 
> If I include the header files from my C++ library, I get a swath of errors,
> namely surrounding the use of 'id' in the subsequent included C++ header
> files.  I also get some more serious errors in the incompatibility between
> X.h (from XFree86) and objc.h over the use of 'BOOL' and 'cursors'.
> 
> Renaming the object file back to .cpp, the project compiles without error,
> so I know it's because of the inclusion of the Objective-C keywords.
> 
> How can I tell XCode to include those headers strictly as C++ headers and
> not Objective-C headers?  I thought using #include instead of #import would
> be clear enough.

It doesn't work that way.
The C/C++/ObjC #include/#import mechanism builds a new text file by dumping in the text of 
each included file at the point where the include occurs. Use XCode's build->preprocess command
to see the text that's actually being sent to the compiler.

You can do things like:

#define cursors xcursors
#include <X.h>
#undef cursors
#include <AppKit/AppKit .h>

to work around conflicts like this.

Note that it may be possible for much of your code to stay pure C++,
and just have some GUI glue that needs to communicate with both Cocoa controls
and your lib be .mm 

Understand also that XWindows is not the MacOSX's native display system,
honestly X.h seems worst case scenario for this.

> 
> Is there a better way of importing C++ headers and libraries into a Obj-C
> project?
> 
> My code is simply thus:
> 
> // main.mm
> 
> #include "userLibrary.h"
> 
> int main (const int argc, char** argv) {
> 
> return 0;
> 
> }
> 
> 
> Which generates a multitude of errors (1700+).
> 
> Compiled as:
> 
> // main.cpp
> 
> #include "userLibrary.h"
> 
> int main (const int argc, char** argv) {
> 
> return 0;
> 
> }
> 
> 
> everything compiles fine.
> 
> Thank you,
> 
> -- 
> -Stephen Furlani
> Biomedical Software Engineer
> e: [email protected]
> c: 240-491-7288
> _______________________________________________
> MacOSX-dev mailing list
> [email protected]
> http://www.omnigroup.com/mailman/listinfo/macosx-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.