如何设置 CMake 为 iPhone 构建应用程序 [英] How to set up CMake to build an app for the iPhone

查看:38
本文介绍了如何设置 CMake 为 iPhone 构建应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我的上一个问题,是关于使用 CMake 在 iPhone 上构建静态库.我得到它来设置 CMAKE_OSX_SYSROOT.

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.

但是,这不适用于构建应用程序.我的 CMakeLists.txt 看起来像:

However, this doesn't work to build an app. My CMakeLists.txt looks like:

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

一些注意事项:

  • 我明确给出了 -framework 链接选项,因为 find_library 不适用于所有框架(它找到了大多数框架,但没有找到 OpenGLES).我不明白为什么,因为它们都在同一个文件夹 ${SDK}/System/Library/Frameworks 中.这让我相信我做错了什么,但我不知道是什么.
  • 我在 add_executable 命令中添加了 MACOSX_BUNDLE,这样生成的产品类型将是 com.apple.product-type.application 而不是com.apple.product-type.tool,这在 iPhone 上显然不存在.
  • I'm explicitly giving the -framework linking option because find_library didn't work for all of the frameworks (it found most of them, but not OpenGLES). 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 the add_executable command so that the product type generated would be com.apple.product-type.application instead of com.apple.product-type.tool, which apparently doesn't exist on the 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.

在 google 和 stackoverflow 上有很多关于此问题的报告实例,但所有解决方案都涉及清理或创建新项目和移动文件;但是我在 CMake 完成工作后编译了一个新副本,所以这些都不适用.

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.

我在 CMake 邮件列表中找到了这个主题,但它只报告成功建立图书馆,然后逐渐消失.

I found this thread on the CMake mailing list, but it only reports a success in building a library, and then peters out.

推荐答案

我终于想出了如何做到这一点.这是我的 CMakeLists.txt 文件的样子:

I finally figured out how to do this. Here's what my CMakeLists.txt file looks like:

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

显然,将mycompany 更改为您的公司名称,并将My Name 更改为您的名称.我发现在上面添加链接目录 ${HOME}/${SDKROOT}/lib 非常有用,这样如果您的应用链接到静态库(尤其是通用(非iPhone) 库),您可以构建单独的 iPhoneOS 和 iPhoneSimulator 库并轻松链接到正确的库,而不必担心通用二进制文件.

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.

此外,当您使用 CMake 构建项目时,Xcode 似乎无法正确添加资源,因此我将这段添加到了 CMakeLists.txt 文件中.它将整个文件夹 /data 复制到我的资源文件夹中(就像我在 Xcode 中添加了一个蓝色"文件夹链接一样).

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

# 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}
)

这篇关于如何设置 CMake 为 iPhone 构建应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆