[CMake] Linking against OS X Frameworks

Michael Wild themiwi at gmail.com
Sat Aug 8 11:43:16 EDT 2009


On 8. Aug, 2009, at 16:08, ML wrote:

> All,
>
> Oh, umm, wait.  How do I specify the 10.4 Universal SDK? Maybe that  
> is it where I specify it on XCode, but I see nothing in my CMake  
> file that would select a particular SDK so would it be using 10.5  
> automatically? How does CMake decide this?
>
> Which would make Michael right that maybe this was introduced in 105.
>
> Sorry for top posting, I thought it might be easier for the way the  
> thread is going.
>
> Thoughts?
>
> -Jason
>

Yep, that would probably be it. What you have to do is setting  
CMAKE_OSX_SYSROOT=/Developer/SDKs/MacOSX10.4u.sdk and CMAKE_C_FLAGS='$ 
{CMAKE_C_FLAGS} -mmacosx-version-min=10.4'. Probably you have to add  
the same to all CMAKE_*_LINKER_FLAGS.

Below CMake code compiles MoreFilesX just fine (most of it is devoted  
to working around issue #6195 and checking that we use a suitable SDK  
and deployment target).

Michael


cmake_minimum_required( VERSION 2.6 )

project( MoreFilesX C )

# construct version if cmake version < 2.6.3
if( NOT DEFINED CMAKE_VERSION )
   set( CMAKE_VERSION "${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.$ 
{CMAKE_PATCH_VERSION}" )
endif( NOT DEFINED CMAKE_VERSION )

# figure out Mac OSX SDK version (do not care for the u-suffix in 10.4u)
get_filename_component( SDK_VER ${CMAKE_OSX_SYSROOT} NAME )
string( REPLACE ".sdk" "" SDK_VER ${SDK_VER} )
string( REPLACE "MacOSX" "" SDK_VER ${SDK_VER} )
string( REGEX REPLACE "[a-zA-Z]" "" SDK_VER ${SDK_VER} )
# this REALLY needs the 10.4 SDK.
if( ${SDK_VER} VERSION_GREATER 10.4 )
   message( SEND_ERROR "MoreFilesX requires the SDK version to be not  
newer than 10.4u (${SDK_VER} detected)" )
endif( ${SDK_VER} VERSION_GREATER 10.4 )

# solution to issue #6195 will hopefully make it into new versions of  
CMake
if( ${CMAKE_VERSION} VERSION_LESS 2.6.5 )
   # default CMAKE_OSX_DEPLOYMENT_TARGET to the SDK version
   set( CMAKE_OSX_DEPLOYMENT_TARGET ${SDK_VER} CACHE STRING
     "Minimum OS X version to target for deployment (at runtime);  
newer APIs weak linked. Set to empty string for default value." )
   foreach( var CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_EXE_LINKER_FLAGS
       CMAKE_SHARED_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS )
     set( ${var} "${${var}} -mmacosx-version-min=$ 
{CMAKE_OSX_DEPLOYMENT_TARGET}" )
   endforeach( var )
endif( ${CMAKE_VERSION} VERSION_LESS 2.6.5 )

# this really needs 10.4 run-time compatibility
if( CMAKE_OSX_DEPLOYMENT_TARGET VERSION_GREATER 10.4 )
   message( SEND_ERROR "MoreFilesX requires  
CMAKE_OSX_DEPLOYMENT_TARGET to be not newer than 10.4" )
endif( CMAKE_OSX_DEPLOYMENT_TARGET VERSION_GREATER 10.4 )

# only want to find frameworks
set( CMAKE_FIND_FRAMEWORK ONLY )
find_library( CARBON_FRAMEWORK Carbon )

# here goes the "actual" stuff
set( MOREFILESX_HEADERS MoreFilesX.h )
set( MOREFILESX_SOURCES MoreFilesX.c )

add_library (MoreFilesX ${MOREFILESX_SOURCES} ${MOREFILESX_HEADERS} )

target_link_libraries (MoreFilesX ${CARBON_FRAMEWORK} )


More information about the CMake mailing list