xcode 如何设置 CMake 为 iPhone 构建库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/794137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up CMake to build a library for the iPhone
提问by Jesse Beder
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:
我正在尝试使用 CMake 通过手动设置某些属性来为 iPhone 生成 Xcode 配置。(这是否是解决此问题的正确方法?)我的 CMake 文件如下所示:
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".
首先,这似乎不起作用 - 在生成的 Xcode 项目(我正在运行cmake . -G Xcode
)中,SDKROOT
仍然没有设置,所以它说“当前的 Mac OS”。
Second, assuming this is the right way to do this, how do I set the attribute only for the configuration Debug-iPhone
?
其次,假设这是正确的方法,我该如何仅为配置设置属性Debug-iPhone
?
回答by richq
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.
据我所知,有两种方法。最有可能的,你就在那里,是CMAKE_OSX_SYSROOT
用来设置 XCode SDKROOT。还有变量CMAKE_OSX_ARCHITECTURES
,它映射到 XCode 中的 ARCHS。
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:
另一种方法是使用CMake 的交叉编译支持。我没有在 iphone 上使用它,但我已经在其他 ARM 处理器上使用过它。您需要设置一个如下所示的工具链文件:
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 时,将CMAKE_TOOLCHAIN_FILE
变量设置为您的工具链文件的名称。或者,如果您只编译为一种架构,则可以对 CMakeLists.txt 文件中的值进行硬编码。但我想你需要为 iphone 模拟器和实际的 iphone 本身交叉编译,对吧?所以你会运行其中一个,可能有几个构建变体:
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.
sim/real 文件定义模拟器或真正的 iphone 编译器工具的环境。
Some other links that may help are this bug reportand this mailing list conversation.
回答by Nick
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.
该项目提供了一个简单的 iOS 工具链文件,可与 CMake 一起使用以构建库和设置应用程序。包括几个示例项目。