Problem when Importing C++ in Objective C

Discussion in 'iOS Programming' started by ta3ban4, Dec 23, 2009.

  1. ta3ban4 macrumors newbie

    Joined:
    Dec 23, 2009
    #1
    Hi All ,
    I'm fairly new to Iphone development. I created View based application and copied C ++ class that i have created a while back to use. First i did drag my h/cpp files to the Classes folder. Changed MyViewController.m File Type to soucecode.cpp.objcpp then #include "eCharBuffer" it compiles fine , but when i try to instantiate/creat an object eCharBuffer mybuff (100,true); it give me the following error ;

    Undefined symbols:
    "eCharBuffer::eCharBuffer(bool)", referenced from:
    -[MapViewController .cxx_construct] in MapViewController.o
    "eCharBuffer::~eCharBuffer()", referenced from:
    -[MapViewController .cxx_destruct] in MapViewController.o

    Unless i define it as a pointer eCharBuffer *mybuff which i dont want to use.

    Any Idea Why and how i can fix . I have a lot of C++ code that i dont want to port to Objective C

    Thanks In Advace
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    Try changing your file extension. Objective-C++ files use the extension "mm", not "cpp" or "objcpp".
     
  3. ta3ban4 thread starter macrumors newbie

    Joined:
    Dec 23, 2009
    #3
    I have changed eCharBuffer.cpp to .mm also the class MapViewController.m to .mm . Still getting the same Error

    Thanks
     
  4. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #4
    Change ALL .m files to .mm. Even those who don't appear to have a C++ class or file included.
     
  5. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #5
    I would double check your code and make sure you are actually implementing the two methods you are referencing. Also make sure that your "eCharBuffer.cpp" is being included in the compilation
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    There is a build setting called 'Call C++ Default Ctors/Dtors in ObjectiveC' that you may need to be turned on.

    Is your c++ class that is causing the problem an ivar of an Obj-C class or are you allocating it on the stack?
     
  7. ta3ban4 thread starter macrumors newbie

    Joined:
    Dec 23, 2009
    #7
    I have been looking at Xcode Build System doc on Apple Dev site made me a but more confused. I did set " Call C++ Default Ctors/Dtors in ObjectiveC" under build/code generation with no success . Not sure what "ivar of an Obj-C class or are you allocating it on the stack" . I'm attaching the h/mm"cpp" files a simple byte buffer class.

    Thanks for the help Guys
     

    Attached Files:

  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    This line

    Is an object of your c++ class an instance variable of an Objective-C class?

    Are you allocating an instance of your c++ class on the stack?
     
  9. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    You can't. It's a requirement of ObjC++ that ObjC classes can not contain instances of C++ objects - only pointers to instances.
     
  10. ta3ban4 thread starter macrumors newbie

    Joined:
    Dec 23, 2009
    #10
    Simply an instance variable of eCharBuff class , which when i tried to initialize in my MapViewController.m class ,myBuff (100, true) was giving me the that error. I tried eCharBuff *mybuff and it did compile. Hope that did help

    Thanks
    Merry Christmas everyone :)
     
  11. ta3ban4 thread starter macrumors newbie

    Joined:
    Dec 23, 2009
    #11
    i got it to compile when using eCharBuff *mybuff , but im getting the following warning
    warning: no rule to process file warning: no rule to process file '$(PROJECT_DIR)/Classes/eCharBuffer.mm' of type sourcecode.cpp for architecture i386




    Any idea ,
    Thanks for the help
     

Share This Page