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

    Jul 26, 2011
    I have written a program in 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 different are objective C files(.m) it possible to incorporate the two different languages?
  2. petermopar macrumors newbie

    Jul 27, 2011
    use .mm and -ObjC++

    i've found yes, i can use c++ files in my obj-c wrapper.
    two steps, rename your main.m ->
    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


    Dec 16, 2010
    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

    Jul 27, 2011
    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


    Sep 13, 2001
    Portland, OR
    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

    May 4, 2011
    Baltimore MD
    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