Can I use c++ files within a cocoa/carbon application?

Discussion in 'Mac Programming' started by paulmcco, Jul 27, 2011.

  1. paulmcco, Jul 27, 2011
    Last edited: Jul 27, 2011

    paulmcco macrumors newbie

    Joined:
    Jul 26, 2011
    #1
    I have written a program in c++...it includes a bunch of header files and whatnot...I would like to build a user interface for this project using cocoa...can this be done? do I use a carbon application? would I have to re-write my c++ project at all?

    I would like to use cocoa because the interface can have more objects...how different are objective C files(.m)...is it possible to incorporate the two different languages?
     
  2. petermopar macrumors newbie

    Joined:
    Jul 27, 2011
    Location:
    baltimore
    #2
    use .mm and -ObjC++

    i've found yes, i can use c++ files in my obj-c wrapper.
    two steps, rename your main.m -> main.mm
    and if using gcc from command line, change -ObjC -> -ObjC++
    i think xcode changes over automatically if you just change the extension.

    i declare my classes completely in their .h files, so i don't know how the linker treats .cpp objects, what flags you need to integrate them right.
     
  3. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    GCC itself will determine the language from the source code extension. There's no need to add -ObjC or -ObjC++. Just name you source files .m or .mm respectively.
     
  4. petermopar macrumors newbie

    Joined:
    Jul 27, 2011
    Location:
    baltimore
    #4
    obj-c and c files

    thnx so much to jiminaus for making the info flow!
    from a noob POV, looks like obj-c and c++ can be used side by side within a c method such as main().

    but of course i don't go passing obj-c instantiations to c++ and vice versa...

    i think of obj-c as java, although it is said that its VM is "thinner, very thin layer", but still it is more like bytecode than c++ which retains a "reality" to its pointers to actual realms within the SRAM... ocj-c is more like the "HANDLE system of win-32 programming?
     
  5. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #5
    You can have pointers ObjC objects in C++, and vice versa. ObjC++ will even call the constructors and destructors of C++ instance variables of ObjC classes for you.

    There's no bytecode, no difference between pointers in ObjC and C++. Your perception is mistaken.
     
  6. stellardream macrumors member

    Joined:
    May 4, 2011
    Location:
    Baltimore MD
    #6
    Clang/LLVM isn't anything like JVM >.< Where do people get that?

    ObjC compiles into native code with Clang/LLVM not some sort of bytecode. LLVM JIT was only used for OpenGL.
     

Share This Page