Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xphere

macrumors newbie
Original poster
Jun 7, 2008
11
0
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!!
 

Attachments

  • test_todelete.zip
    54.3 KB · Views: 416
  • c++_class_error.png
    c++_class_error.png
    100.6 KB · Views: 2,081

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

xphere

macrumors newbie
Original poster
Jun 7, 2008
11
0
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?
 

Attachments

  • test_todelete.zip
    55.1 KB · Views: 525
  • mixCppandObjC.png
    mixCppandObjC.png
    159.4 KB · Views: 1,923

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

xphere

macrumors newbie
Original poster
Jun 7, 2008
11
0
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... :(
 

Attachments

  • Imagen 3.png
    Imagen 3.png
    100.6 KB · Views: 1,257

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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).
 

xphere

macrumors newbie
Original poster
Jun 7, 2008
11
0
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!!!
 

Attachments

  • Imagen 3.png
    Imagen 3.png
    98 KB · Views: 1,405

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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
 

xphere

macrumors newbie
Original poster
Jun 7, 2008
11
0
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?

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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.