25

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)

4 Answers 4

16

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")
3
  • 1
    not works on cmake 2.8.11 errors: CMake Error: Could not find cmake module file:/Users/leanidchaika/job/lge/build/CMakeFiles/2.8.11.2/CMakeCCompiler.cmake CMake Error: Could not find cmake module file:/Users/leanidchaika/job/lge/build/CMakeFiles/2.8.11.2/CMakeCXXCompiler.cmake Commented Oct 15, 2013 at 12:44
  • 2
    Here's a bit more recent fork of the linked toolchain: github.com/cristeab/ios-cmake I ran into a couple of bugs with the old version that have been fixed in this one Commented Sep 27, 2016 at 12:52
  • 1
    Found even newer fork with 1K+ stars on GitHub!!
    – Top-Master
    Commented Jun 5, 2021 at 19:23
16

By following cmake-toolchains documentation I did like below:

cmake -G Xcode -B build \
    -DCMAKE_SYSTEM_NAME=iOS \
    -DCMAKE_Swift_COMPILER_FORCED=true \
    -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0

Note: That assignment CMAKE_OSX_DEPLOYMENT_TARGET=11.0 is not a mistake when targeting iOS.

1
  • I am using coturn master for Turn client library. I have successfully generated cmake build but the file libturnutils.a is mac os compatible. What changes should I make to make it cmpatible for ios?
    – Manthan
    Commented Jun 16, 2021 at 8:37
3

I've been using an updated version of the iOS CMake toolchain for quite a while: https://github.com/leetal/ios-cmake

This creates an Xcode project which you can drag into an existing iOS project if necessary.

I wrote a blog post with more details: https://blog.tomtasche.at/2019/05/how-to-include-cmake-project-in-xcode.html

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 ( )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.