Search code examples
cocoaxcodegccmacos-carbon

HICococaView not compiling with GCC 4.2


We have a large Carbon based (PowerPlant) application that we are looking finally to port over to Cocoa. We will be doing this incrementally and a first step is to try to get a Cocoa view into a Carbon window.

The problem seems to be that when I use any of the functions from HICocoaView.h the application will not compile unless I switch the compiler from GCC 4.2 to GCC 4.0.

Using any compiler other than GCC 4.0 I get an error in XCode that the functions are unavailable e.g. "HICocoaViewCreate is unavailable".

I can't figure out why this won't work, will we have to switch to the older compiler or is there some setting we can change to get it to compile?

Any help or pointers to useful documentation on porting Carbon to Cocoa greatly appreciated. I've read through the old Carbon Cocoa Integration guide but it doesn't mention this.

Edit: As requested here's the output from the build for the gcc command line:-

/Developer/usr/bin/gcc-4.2 -x objective-c++ -arch i386 -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wmissing-prototypes -Wreturn-type -Wunused-variable -Wunused-value -D__IMPRO_DEBUG_BUILD__ -isysroot /Developer/SDKs/MacOSX10.5.sdk -mfix-and-continue -mmacosx-version-min=10.5 -gdwarf-2 "-I/Users/matt/Code/MattsFeatureBranch/Modules/User Notes/Mac/../../../(Intermediates)/Debug/User Notes.build/Debug/Module Bundle.build/User Notes.hmap" -Wparentheses -Wno-conversion -Wno-sign-compare -Wno-multichar -Wno-deprecated-declarations "-F/Users/matt/Code/MattsFeatureBranch/Modules/User Notes/Mac/../../../Build Products/Mac/Debug/Plugins" "-F../../../Build Products/Mac/Debug" "-F../../../Third Party/Mac/NVidia" "-I/Users/matt/Code/MattsFeatureBranch/Modules/User Notes/Mac/../../../Build Products/Mac/Debug/Plugins/include" -I../X-Platform -I../../../Common/Mac -I../../../Common/X-Platform -I../../../DLLs/ArcadiaCore/Mac -I../../../DLLs/ArcadiaCore/X-Platform "-I../../../Third Party/Mac/Powerplant" -I/Developer/SDKs/MacOSX10.5.sdk/Developer/Headers/FlatCarbon "-I../../../Third Party/X-Platform/boost_1_38_0" -I../../../DLLs/ArcadiaImaging/Mac -I../../../DLLs/ArcadiaImaging/X-Platform -I../../../DLLs/ArcadiaDatabase/Mac -I../../../DLLs/ArcadiaDatabase/X-Platform -I../../../DLLs/ArcadiaUI/Mac -I../../../DLLs/ArcadiaUI/X-Platform "-I../../../Third Party/Mac/Powerplant Extras" -I../../../DLLs/ArcadiaDevices/Mac -I../../../DLLs/ArcadiaDevices/X-Platform -I../../../DLLs/Arcadia3D/Mac -I../../../DLLs/Arcadia3D/X-Platform "-I/Users/matt/Code/MattsFeatureBranch/Modules/User Notes/Mac/../../../(Intermediates)/Debug/User Notes.build/Debug/Module Bundle.build/DerivedSources/i386" "-I/Users/matt/Code/MattsFeatureBranch/Modules/User Notes/Mac/../../../(Intermediates)/Debug/User Notes.build/Debug/Module Bundle.build/DerivedSources" -fpermissive -fasm-blocks -include "/Users/matt/Code/MattsFeatureBranch/Modules/User Notes/Mac/../../../(Intermediates)/Debug/SharedPrecompiledHeaders/XPrefix-acshmfbgvfwrdqbyayvgnckkypgr/XPrefix.h" -c "/Users/matt/Code/MattsFeatureBranch/Modules/User Notes/Mac/MUserNotesView.cpp" -o "/Users/matt/Code/MattsFeatureBranch/Modules/User Notes/Mac/../../../(Intermediates)/Debug/User Notes.build/Debug/Module Bundle.build/Objects-normal/i386/MUserNotesView.o"


Solution

  • From HICocoaView.h in both the 10.5 and the 10.6 SDK:

    #if !__LP64__
    extern OSStatus
    HICocoaViewCreate(
      NSView *     inNSView,        /* can be NULL */
      OptionBits   inOptions,
      HIViewRef *  outHIView)                                                       AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
    …
    #endif  /* !__LP64__ */
    

    This means that HICocoaViewCreate() is not available on 64-bit (LP64) targets, i.e., if you need to use this function you have to target i386 (or PowerPC).

    GCC 4.0 targets i386 by default even when run on 64-bit capable machines. On the other hand, GCC 4.2 targets x86_64 by default on 64-bit machines:

    $ gcc-4.0 a.c; lipo -info a.out
    Non-fat file: a.out is architecture: i386
    
    $ gcc-4.2 a.c; lipo -info a.out
    Non-fat file: a.out is architecture: x86_64
    

    If you want to use both HICocoaViewCreate() and GCC 4.2, tell it to create (and use) 32-bit objects/binaries by passing -arch i386. For instance,

    $ gcc-4.2 a.c -arch i386; lipo -info a.out
    Non-fat file: a.out is architecture: i386
    

    Even though part of Carbon is available for 64-bit targets, you’ll notice in the 64-bit Guide for Carbon Developers that much of HIToolbox simply isn’t available.

    As for migrating from Carbon to Cocoa, it’s a whole new Objective-C API for the most part. I’m not aware of any simple migration guide, and Peter Hosey’s answer to a similar question on Stack Overflow is worth reading.