Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 7, 2008, 12:57 PM   #1
xphere
macrumors newbie
 
Join Date: Jun 2008
Mixing C++ and ObjectiveC

hi all!

I'm developing a tool with Objective-C/Cocoa, that should use C++ code.

Basically the front end it is in objC, but the core is in C++... the problem is that seems that the code cannot be mixed, at least as I expected... each time I call a CPP '.h' file, it crashes...
I've tried to add Carbon framework without success... I've prepared a small project to illustrate the problem

Thanks in advance!!
Attached Thumbnails
Click image for larger version

Name:	c++_class_error.png
Views:	1830
Size:	100.6 KB
ID:	119047  
Attached Files
File Type: zip test_todelete.zip (54.3 KB, 277 views)

Last edited by xphere; Jun 7, 2008 at 01:03 PM.
xphere is offline   0 Reply With Quote
Old Jun 7, 2008, 01:16 PM   #2
stijnschoor
macrumors newbie
 
Join Date: Jun 2008
You should try Objective-C++, just change the extension to .mm
I hope it helped!
Good luck
stijnschoor is offline   0 Reply With Quote
Old Jun 7, 2008, 02:58 PM   #3
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Yup, you need to change main.m to main.mm because it includes the cpp class definition of class caca.You can keep the .cpp extension for caca.cpp though.

b e n
lazydog is offline   0 Reply With Quote
Old Jun 8, 2008, 03:35 PM   #4
xphere
Thread Starter
macrumors newbie
 
Join Date: Jun 2008
Hi all! thanks for your answers, they were really helpful, but the problem now comes when I try to include a CPP header from an ObjC header.
The CPP core files can be all included with one single header file. So I just need to call this .h (CPP format) from another .h file (in ObjC format).

I've attached a screenshot and the project to illustrate the problem...
In this example you will see that my ObjectiveC class (called 'objc_class') is trying to include a C++ class (called 'caca'), because it should use it (with the variable 'my_c_class').

Any tricks?
Attached Thumbnails
Click image for larger version

Name:	mixCppandObjC.png
Views:	1669
Size:	159.4 KB
ID:	119264  
Attached Files
File Type: zip test_todelete.zip (55.1 KB, 272 views)
__________________
xphere - code or die
MBP - late 2006 - C2D 2,16MHz - ATIX1600 - 4Gb - SSD+HDD (no Optical drive!)
http://www.xphere.me
xphere is offline   0 Reply With Quote
Old Jun 8, 2008, 04:09 PM   #5
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Same problem again, any file that includes obj_class.h will have to have a .mm extension. The simplest solution is to give all your source files a .mm extension. Either that or change the preferences to push all .m files through the Objective-C++ compiler.

b e n
lazydog is offline   0 Reply With Quote
Old Jun 8, 2008, 05:41 PM   #6
xphere
Thread Starter
macrumors newbie
 
Join Date: Jun 2008
I've tried changing main.m to main.mm, and add an import to my objectivec_class but i get a strange error (see screenshot).
And... if one ObjC header (.h) is calling a C++ header, how can I make that a header is treated with the ObjectiveC++ compiler?

I've tried changing all .m files to .mm ('main.mm' and 'objc_class.mm'), and also changing the 'filetype' in file properties, without any success...
Attached Thumbnails
Click image for larger version

Name:	Imagen 3.png
Views:	1041
Size:	100.6 KB
ID:	119283  
__________________
xphere - code or die
MBP - late 2006 - C2D 2,16MHz - ATIX1600 - 4Gb - SSD+HDD (no Optical drive!)
http://www.xphere.me

Last edited by xphere; Jun 8, 2008 at 05:49 PM.
xphere is offline   0 Reply With Quote
Old Jun 8, 2008, 07:51 PM   #7
yeroen
macrumors 6502a
 
yeroen's Avatar
 
Join Date: Mar 2007
Location: Cambridge, MA
try changing the '.h' extension to a '.hpp' extension
__________________
all Jarvis, all the time
yeroen is offline   0 Reply With Quote
Old Jun 8, 2008, 07:54 PM   #8
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Couple of things wrong here:-

First you're trying to define a class called objc_class which is already defined by the system as a struct, (spotlight objc_class and you'll see objc.h).

Second, you're defining an instance variable:-

Code:
@interface objc_class : NSObject
{
	caca my_c_class;
}
This doesn't work because caca has a virtual destructor. If you make the destructor real then it should be okay. Have a look at page 99 of the Objective-C reference.

b e n


Oh, and rename objc_class.m to objc_class.mm (but you want it something different from objc_class anyway).
lazydog is offline   0 Reply With Quote
Old Jun 10, 2008, 02:50 PM   #9
xphere
Thread Starter
macrumors newbie
 
Join Date: Jun 2008
Thank you so much guys!!!
The major issue that I had was what lazydog said: objc_class was an already defined class!!
Now everything is running like a charm!! thank you so much to everyone!!!
Attached Thumbnails
Click image for larger version

Name:	Imagen 3.png
Views:	1228
Size:	98.0 KB
ID:	119556  
__________________
xphere - code or die
MBP - late 2006 - C2D 2,16MHz - ATIX1600 - 4Gb - SSD+HDD (no Optical drive!)
http://www.xphere.me
xphere is offline   0 Reply With Quote
Old Jun 10, 2008, 07:27 PM   #10
gnasher729
In Time-Out
 
Join Date: Nov 2005
First, forget about using a C++ object as a member of an Objective-C object. That is just asking for trouble (getting constructors/destructors called at the right time is tricky). What is much easier and much less of a problem is to have _a pointer_ to a C++ object in an Objective-C object.

Second, you can change your C++ header file so that it can be used from C or Objective-C code. Just write something like

#ifdef __cplusplus
class myclass { ... };
#else
typedef struct myclass__ myclass;
#endif
gnasher729 is offline   0 Reply With Quote
Old Jun 13, 2008, 01:35 PM   #11
xphere
Thread Starter
macrumors newbie
 
Join Date: Jun 2008
Quote:
Originally Posted by gnasher729 View Post
First, forget about using a C++ object as a member of an Objective-C object. That is just asking for trouble (getting constructors/destructors called at the right time is tricky). What is much easier and much less of a problem is to have _a pointer_ to a C++ object in an Objective-C object.
"have a pointer to a C++ object in a ObjC object".... I really don't get your point, how can I do that?

Quote:
Originally Posted by gnasher729 View Post
Second, you can change your C++ header file so that it can be used from C or Objective-C code. Just write something like

#ifdef __cplusplus
class myclass { ... };
#else
typedef struct myclass__ myclass;
#endif
Yes, that could be very helpful, but (by now), C++ files are untouchable, because are part from another project :S I want to make sure that my objectiveC code can use the C++ library without touching a single line of C++ code... then I'll go for optimisations
__________________
xphere - code or die
MBP - late 2006 - C2D 2,16MHz - ATIX1600 - 4Gb - SSD+HDD (no Optical drive!)
http://www.xphere.me
xphere is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Mixing TB 2 and TB 1 Cubemmal Mac Pro 14 Dec 29, 2013 02:06 PM
mixing music barriem iPad Tips, Help and Troubleshooting 1 Sep 14, 2013 01:43 AM
Mixing ram Shmuco MacBook Pro 1 Jul 7, 2013 07:12 AM
Mixing Ram? alecmcmahon iMac 28 Jan 11, 2013 09:44 PM
Mixing RAM mikeycol Mac mini 3 Dec 5, 2012 10:50 PM

Forum Jump

All times are GMT -5. The time now is 03:39 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC