Mixing Objective C with C++

Discussion in 'Mac Programming' started by mlopres, Jan 12, 2010.

  1. mlopres macrumors newbie

    Jan 12, 2010
    First of all, pardon my complete newbness as I'm completely new to developing on MAC along with Cocoa, and Obj-C

    So I'm doing some very basic trial and error testing, as I get accustomed to Objective C.

    Right now i have two simple classes. I have my model class which adds two numbers, and is written in C++. I also wrote a Controller class written in Objective C, which will handle functions when a Button is pressed on the GUI and will contain an instance of the model class.

    My question is, how can I succesfully compile this project, as the errors I'm getting lead me to believe I'm missing something in order to successfully mix the two languages.


    P.S. I've tried renaming the ModelClass.cpp to ModelClass.mm and nothing happened.
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Rename your controller class to .mm as well. If it contains a C++ object, it needs to be compiled as Objective-C++. if your c++ class contains no objective-C, it can stay .cpp.

  3. mlopres thread starter macrumors newbie

    Jan 12, 2010

    that fixed the issue.


    One, other quick question. When I'm working within the Controller class and using the model class instance, which language syntax do I use.
  4. Sander macrumors 6502

    Apr 24, 2008
    If you're using a C++ object from within Objective-C code, you use the C++ syntax. If you're using an Objective-C object from withing C++ code, you use the Objective-C syntax.

    It's really mix-and-match, with only a few exceptions (member variables in one language inside objects of another have a few restrictions).
  5. Detrius macrumors 68000

    Sep 10, 2008
    Asheville, NC
    When you call a Macintosh a MAC, you sound like you've barely ever even heard of them. It's not an acronym. It's a Mac.

    MAC = Medium Access Control, something that's used at the physical ethernet layer. Don't confuse these. It annoys people... about like confusing "there," "their," and "they're." ;)

Share This Page