Mixing C++ and ObjectiveC

Discussion in 'Mac Programming' started by xphere, Jun 7, 2008.

  1. macrumors newbie

    Joined:
    Jun 7, 2008
    #1
    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 Files:

  2. macrumors newbie

    Joined:
    Jun 7, 2008
    #2
    You should try Objective-C++, just change the extension to .mm
    I hope it helped!
    Good luck
     
  3. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    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
     
  4. thread starter macrumors newbie

    Joined:
    Jun 7, 2008
    #4
    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 Files:

  5. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    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
     
  6. thread starter macrumors newbie

    Joined:
    Jun 7, 2008
    #6
    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 Files:

  7. macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #7
    try changing the '.h' extension to a '.hpp' extension
     
  8. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #8
    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).
     
  9. thread starter macrumors newbie

    Joined:
    Jun 7, 2008
    #9
    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 Files:

  10. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    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
     
  11. thread starter macrumors newbie

    Joined:
    Jun 7, 2008
    #11
    "have a pointer to a C++ object in a ObjC object".... I really don't get your point, how can I do that?

    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 :)
     

Share This Page