Skip to content

Instantly share code, notes, and snippets.

@sixman9
Created December 14, 2010 10:42
Show Gist options
  • Save sixman9/740257 to your computer and use it in GitHub Desktop.
Save sixman9/740257 to your computer and use it in GitHub Desktop.
cmake files for Apple IOS development (has C++ lean, can be adapted however)
# See original post at http://stackoverflow.com/questions/822404/how-to-set-up-cmake-to-build-an-app-for-the-iphone
cmake_minimum_required(VERSION 2.8)
cmake_policy(SET CMP0015 NEW)
cmake_policy(SET CMP0016 NEW)
project(test)
set(NAME test)
file(GLOB headers *.h)
file(GLOB sources *.cpp)
SET (SDKVER "4.1")
SET (DEVROOT "/Developer/Platforms/iPhoneOS.platform/Developer")
SET (SDKROOT "${DEVROOT}/SDKs/iPhoneOS${SDKVER}.sdk")
SET (CMAKE_OSX_SYSROOT "${SDKROOT}")
SET (CMAKE_OSX_ARCHITECTURES "$(ARCHS_UNIVERSAL_IPHONE_OS)")
#Other 'CMAKE_OSX_ARCHITECTURES' iPhone/IOS option examples
#SET (CMAKE_OSX_ARCHITECTURES "armv6" "armv7")
#SET (CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD_32_BIT))
set(CMAKE_CXX_FLAGS "-x objective-c++")
set(CMAKE_EXE_LINKER_FLAGS
"-framework AudioToolbox -framework CoreGraphics -framework QuartzCore -framework UIKit"
)
link_directories(\${HOME}/\${SDKROOT}/lib)
set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.mycompany.\${PRODUCT_NAME:identifier}")
set(APP_TYPE MACOSX_BUNDLE)
add_executable(${NAME}
${APP_TYPE}
${headers}
${sources}
)
target_link_libraries(${NAME}
# other libraries to link
)
# code signing
set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer: My Name")
@sixman9
Copy link
Author

sixman9 commented Dec 14, 2010

CMake-based IOS resource copying (see StackOverflow post )

Xcode doesn't seem to properly add resources when you build the project using CMake, so I this can be added to the CMakeLists.txt file:

# copy resource phase

set(APP_NAME \${TARGET_BUILD_DIR}/\${FULL_PRODUCT_NAME})
set(RES_DIR ${test_SOURCE_DIR}/data)
add_custom_command(
    TARGET ${NAME}
    POST_BUILD
    COMMAND /Developer/Library/PrivateFrameworks/DevToolsCore.framework/Resources/pbxcp -exclude .DS_Store -exclude CVS -exclude .svn -resolve-src-symlinks ${RES_DIR} ${APP_NAME}
)

This will copy the entire folder /data into the resources folder (as if we had added a "blue" folder link in Xcode).

Finding IOS Frameworks programmatically within CMake

The following macro snippet [supposedly] can be used in place of the 'CMAKE_EXE_LINKER_FLAGS' property and arguments

SET(TARGETSDK iPhoneOS4.1.sdk)
SET(CMAKE_OSX_SYSROOT /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/${TARGETSDK})
macro(ADD_FRAMEWORK fwname appname)
    find_library(FRAMEWORK_${fwname}
        NAMES ${fwname}
        PATHS ${CMAKE_OSX_SYSROOT}/System/Library
        PATH_SUFFIXES Frameworks
        NO_DEFAULT_PATH)
    if( ${FRAMEWORK_${fwname}} STREQUAL FRAMEWORK_${fwname}-NOTFOUND)
        MESSAGE(ERROR ": Framework ${fwname} not found")
    else()
        TARGET_LINK_LIBRARIES(${appname} ${FRAMEWORK_${fwname}})
        MESSAGE(STATUS "Framework ${fwname} found at ${FRAMEWORK_${fwname}}")
    endif()
endmacro(ADD_FRAMEWORK)

Once the macro is in place, the framework can be added thus:

#It might be possible to use the ${NAME} variable value (i.e. substituted for the 'MyApp' name)
ADD_FRAMEWORK(AudioToolbox MyApp)
ADD_FRAMEWORK(CoreGraphics MyApp)
ADD_FRAMEWORK(QuartzCore MyApp)
ADD_FRAMEWORK(UIKit MyApp)

Making CMake produce universal Iphone binaries

Just set the architecture variable to:

set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_UNIVERSAL_IPHONE_OS)")

This will generate armv6/armv7 etc binaries. There are some posts out there (stackoverflow etc.) which say that this has caused problems with iPad deployment, however, I am unable to verify that at present.

@sixman9
Copy link
Author

sixman9 commented Dec 14, 2010

Load a file's content into a CMake variable (parsing properties is the grail...)

SET(cat_prog cat)
IF(WIN32)
  IF(NOT UNIX)
    SET(cat_prog type)
  ELSE(NOT UNIX)
ENDIF(WIN32)

EXEC_PROGRAM(${cat_prog} ARGS /input/file.txt OUTPUT_VARIABLE variable)

MESSAGE("Content of file is: ${variable}")

@Amicoolll
Copy link

Hi Guys,

Really Appreciate for this solution.

Will this work with making multiple projects for 1 code as well?

Thanks in Advance.

Thanks & Regards
Amit Singh

@newNcy
Copy link

newNcy commented Aug 16, 2021

do you know how to link an existing framework?
你知道怎么链接一个已经存在的framework吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment