Compiling for iOS with CMake

2019-03-11 06:34发布

问题:

I've compiled a C++ static library by using CMake as my building tool and I want to link it to my iOS app.
I created a simple 'Empty' application in Xcode and linked my library called libengine.a to it.
I tried to compile my iOS project and the linker gave me this warning:

ignoring file /Users/.../build/engine/libengine.a, 
file was built for archive which is not the architecture being linked (i386):
/Users/.../build/engine/libengine.a

As I understand it, I need to compile my library for ARM processors. The problem is I don't know how.
I think CMake really lacks good tutorials.
Anyways, my CMake scripts are attached below.

Any help would be greatly appreciated.
Thanks, Tal.

Here is my main CMake script:

cmake_minimum_required(VERSION 2.8)

project(movie-night)

if (DEFINED PLATFORM)
    include(toolchains/ios.cmake)
endif()

add_definitions(-Wall)

set(DEBUG)

if (DEFINED DEBUG)
    add_definitions(-g)
endif()

if (DEFINED RELEASE)
    add_definitions(-O3)
endif()

add_subdirectory(engine)
add_subdirectory(ui)

add_subdirectory(test)

Here is my toolchains/ios.cmake file:

set(CMAKE_SYSTEM_NAME Darwin)
set(CMAKE_SYSTEM_PROCESSOR arm)

回答1:

Just use this toolchain file: http://code.google.com/p/ios-cmake/ and use it as

cmake -DCMAKE_TOOLCHAIN_FILE=path_to_your_toolchain_file

Then, in CMakeLists.txt:

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch armv7")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch armv7")


回答2:

There is a second version of iOS.cmake located at:

https://ceres-solver.googlesource.com

Note: You may find that both versions of iOS.cmake will only build x86 versions of your project. CMake now sets CMAKE_OSX_SYSROOT to the (latest) Mac OS X SDK available on your system. You can fix this by modifying your copy of iOS.cmake to always set CMAKE_OSX_SYSROOT. You can do this by commenting out a couple of lines your copy of iOS.cmake:

# -- Under CMake 3.4.2, CMAKE_OSX_SYSROOT is automatically defined to point to the latest Mac OS X SDK. --
# -- So, the iOS SDK is never found.  Grab the correct CMAKE_OS_SYSROOT and ignore any prior setting.   --

# If user did not specify the SDK root to use, then query xcodebuild for it.
# if (NOT CMAKE_OSX_SYSROOT)
  execute_process(COMMAND xcodebuild -version -sdk ${XCODE_IOS_PLATFORM} Path
    OUTPUT_VARIABLE CMAKE_OSX_SYSROOT
    ERROR_QUIET
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  message (STATUS "Using SDK: ${CMAKE_OSX_SYSROOT} for platform: ${IOS_PLATFORM}")
  message (STATUS "be sure the previous line points to the correct SDK")
# endif ( )