Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up CMake to build a library for the iPhone

I'm trying to use CMake to generate an Xcode configuration for the iPhone by manually setting certain attributes. (Is this even the right way to go about this?) My CMake file looks like:

project(MYLIB)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_CONFIGURATION_TYPES Debug Release Debug-iPhone)
set(FILES list of my files...)

add_library(mylib FILES)

set(XCODE_ATTRIBUTE_SDKROOT iphoneos2.2.1)
# more attributes later, I'm just trying to get one to work first

First of all, this doesn't seem to work - in the generated Xcode project (I'm running cmake . -G Xcode), SDKROOT is still set to nothing, and so it says "Current Mac OS".

Second, assuming this is the right way to do this, how do I set the attribute only for the configuration Debug-iPhone?

like image 206
Jesse Beder Avatar asked Apr 27 '09 15:04

Jesse Beder


People also ask

How do I set up a build on CMake?

Run the cmake executable or the cmake-gui to configure the project and then build it with your chosen build tool. Run the install step by using the install option of the cmake command (introduced in 3.15, older versions of CMake must use make install ) from the command line, or build the INSTALL target from an IDE.

Can I use CMake instead of make?

If you have Windows developers used to Visual Studio and Unix developers who swear by GNU Make, CMake is (one of) the way(s) to go. I would always recommend using CMake (or another buildsystem generator, but CMake is my personal preference) if you intend your project to be multi-platform or widely usable.

Where can I build CMake binaries?

CMake places built binary files as well as build sideproducts in a build folder that should be separate from the source directory. To copy only the needed files (and additional library files they depend on) to a folder you can share with colleagues or copy onto another PC, you need to 'install' them.

What is a build system CMake?

CMake is not a build system itself; it generates another system's build files. It supports directory hierarchies and applications that depend on multiple libraries. It is used in conjunction with native build environments such as Make, Qt Creator, Ninja, Android Studio, Apple's Xcode, and Microsoft Visual Studio.


2 Answers

As far as I can tell, there are two ways. The one most likely, you are nearly there, is to use CMAKE_OSX_SYSROOT to set the XCode SDKROOT. There is also the variable CMAKE_OSX_ARCHITECTURES, which maps to ARCHS in XCode.

The alternative is to use CMake's cross compiling support. I've not used it for the iphone, but I have done so for other ARM processors. You need to set up a toolchain file that would look something like this:

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm-elf)
set(CMAKE_C_COMPILER /path/to/complier/bin/arm-elf-gcc)
set(CMAKE_FIND_ROOT_PATH /path/to/compiler)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

And then when you run cmake, set the CMAKE_TOOLCHAIN_FILE variable to the name of your toolchain file. Or if you only compile to one architecture, you can hard-code the value in the CMakeLists.txt file. But I imagine you would need to cross compile for the iphone simulator and for the actual iphone itself, right? So you would run one of these, probably having a couple of build variants:

cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/my/iphone-sim.cmake .
cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/my/iphone-real.cmake .

Where the sim/real files define the environment for the simulator or real iphone compiler tools.

Some other links that may help are this bug report and this mailing list conversation.

like image 164
richq Avatar answered Oct 04 '22 04:10

richq


This info may also be useful:

This project provides a simple iOS toolchain file that may be used with CMake to build libraries and setup applications. A couple of sample projects are included.

http://code.google.com/p/ios-cmake/

like image 31
Nick Avatar answered Oct 04 '22 02:10

Nick