xcode 如何设置 CMake 为 iPhone 构建应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/822404/
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 an app for the iPhone
提问by Jesse Beder
This is closely related to my previous question, which was about using CMake to build a static library on the iPhone. I got that to work setting the CMAKE_OSX_SYSROOT
.
这与我之前的问题密切相关,该问题是关于使用 CMake 在 iPhone 上构建静态库。我得到了它的工作设置CMAKE_OSX_SYSROOT
.
However, this doesn't work to build an app. My CMakeLists.txt
looks like:
但是,这不适用于构建应用程序。我的CMakeLists.txt
样子:
project(TEST)
set(CMAKE_OSX_SYSROOT iphoneos2.2.1)
set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_32_BIT)")
set(CMAKE_EXE_LINKER_FLAGS
"-framework Foundation -framework OpenGLES -framework AudioToolbox -framework CoreGraphics -framework QuartzCore -framework UIKit -framework OpenAL"
)
set(SRC --my files--)
add_executable(iphone-test MACOSX_BUNDLE ${SRC})
A few notes:
一些注意事项:
- I'm explicitly giving the
-framework
linking option becausefind_library
didn't work for all of the frameworks (it found most of them, but notOpenGLES
). I don't understand why, since they're all in the same folder${SDK}/System/Library/Frameworks
. This leads me to believe that I was doing something wrong, but I don't know what. - I added
MACOSX_BUNDLE
to theadd_executable
command so that the product type generated would becom.apple.product-type.application
instead ofcom.apple.product-type.tool
, which apparently doesn't exist on the iPhone.
- 我明确给出了
-framework
链接选项,因为它find_library
不适用于所有框架(它找到了大多数框架,但没有找到OpenGLES
)。我不明白为什么,因为它们都在同一个文件夹中${SDK}/System/Library/Frameworks
。这让我相信我做错了什么,但我不知道是什么。 - 我添加
MACOSX_BUNDLE
到add_executable
命令中,以便生成的产品类型com.apple.product-type.application
代替com.apple.product-type.tool
,这在 iPhone 上显然不存在。
In any case, the app compiles and links correctly, but when I run it in the simulator, I get the dreaded
在任何情况下,该应用程序都可以正确编译和链接,但是当我在模拟器中运行它时,我感到害怕
Failed to launch simulated application: Unknown error.
There are lots of reported instances of this problem on google and stackoverflow, but all of the solutions involve cleaning up or creating a new project and moving files; but I'm compiling a fresh copy after CMake does its work, so none of that applies.
在 google 和 stackoverflow 上有很多关于这个问题的报告实例,但所有的解决方案都涉及清理或创建一个新项目和移动文件;但是我在 CMake 完成工作后编译了一个新副本,所以这些都不适用。
I found this threadon the CMake mailing list, but it only reports a success in building a library, and then peters out.
我在 CMake 邮件列表上找到了这个线程,但它只报告成功构建了一个库,然后逐渐消失。
回答by Jesse Beder
I finally figured out how to do this. Here's what my CMakeLists.txt
file looks like:
我终于想出了如何做到这一点。这是我的CMakeLists.txt
文件的样子:
project(test)
set(NAME test)
file(GLOB headers *.h)
file(GLOB sources *.cpp)
set(CMAKE_OSX_SYSROOT iphoneos2.2.1)
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")
Obviously, change mycompany
to your company name, and change My Name
to your name. I found it's very useful to add the link directory \${HOME}/\${SDKROOT}/lib
as above, so that if your app links to a static library (especially a generic (non-iPhone) library), you can build separate iPhoneOS and iPhoneSimulator libraries and easily link to the right one, instead of worrying about a universal binary.
显然,更改mycompany
为您的公司名称,并更改My Name
为您的姓名。我发现添加\${HOME}/\${SDKROOT}/lib
上面的链接目录非常有用,这样如果您的应用程序链接到静态库(尤其是通用(非 iPhone)库),您就可以构建单独的 iPhoneOS 和 iPhoneSimulator 库并轻松链接到正确的库,而不是担心通用二进制文件。
Also, Xcode doesn't seem to properly add resources when you build the project using CMake, so I added this piece to the CMakeLists.txt
file. It copies the entire folder /data
into my resources folder (as if I had added a "blue" folder link in Xcode).
此外,当您使用 CMake 构建项目时,Xcode 似乎没有正确添加资源,因此我将这部分添加到CMakeLists.txt
文件中。它将整个文件夹复制/data
到我的资源文件夹中(就像我在 Xcode 中添加了一个“蓝色”文件夹链接一样)。
# 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}
)
回答by mtoy
For frameworks, I found this message http://www.mail-archive.com/[email protected]/msg24659.htmlI grabbed enough information for this:
对于框架,我发现此消息http://www.mail-archive.com/[email protected]/msg24659.html我为此获取了足够的信息:
SET(TARGETSDK iPhoneOS3.1.2.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)
Instead of setting CMAKE_EXE_LINKER_FLAGS, now I do:
现在我没有设置 CMAKE_EXE_LINKER_FLAGS,而是:
ADD_FRAMEWORK(AudioToolbox MyApp)
ADD_FRAMEWORK(CoreGraphics MyApp)
ADD_FRAMEWORK(QuartzCore MyApp)
ADD_FRAMEWORK(UIKit MyApp)
This does work for OpenGLES as well.
这也适用于 OpenGLES。
回答by mtoy
Recent CMake versions pass .m and .mm files to the C++ compiler, which detects the language through the extension, so you don't need to add "-x objective-c++" to the flags explicitly.
最近的 CMake 版本将 .m 和 .mm 文件传递给 C++ 编译器,C++ 编译器通过扩展检测语言,因此您不需要明确地将“-x Objective-c++”添加到标志中。