Re: Mixing Objective-C and C++ in Xcode
Re: Mixing Objective-C and C++ in Xcode
- Subject: Re: Mixing Objective-C and C++ in Xcode
- From: Scott Thompson <email@hidden>
- Date: Fri, 2 Jul 2004 10:25:19 -0500
UI front end. So at some point I have a Obj-C header including a C++
header so I can access methods and data in that C++ code via the Obj-C
You should likely be including the c++ header file from within the
source file, not the Obj-C header. Also, any source file that is
including the c++ header file needs to be compiled as c++ (or
obj-c++).
Let's say you have:
foo.h
foo.cpp
bar.h
bar.m
and you want to use a class declared in foo in your bar.m. You should
rename bar.m to bar.mm and bar.mm should #include "foo.h". bar.h
Should not include foo.h, especially if it is going to be included by
another objective-c source file. Otherwise that one would need to be
.mm as well.
Informationally, you can also name your file bar.M (with a capitol M)
and the system will use the objective-C++ compiler. That's a bit less
reliable than the ".mm" extension since HFS+ is case-insenstive and
will treat foo.M and foo.m as the same file. It works, nontheless.
Another option is to bring up the project build settings and look under
the "Copile Sources As" setting (use the search field if you like). In
this setting you can force the compiler to compile all your sources as
objective-C++ regardless of their file type extensions.
Another useful tool in working with Objective-C++ is to use compiler
macros:
#ifdef cplusplus
... insert ++ code here
#endif
#if __OBJC__
...insert objective-C code here...
#endif
the macros can shield your objective-C code from the C++ compiler or
vice-versa. This is NOT what the original poster was asking for (in
that case the C++ should have been visible) but it is a handy technique
in some cases.
Scott
_______________________________________________
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.