本文介绍了如何设置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

  • 我将 MACOSX_BUNDLE 添加到了 add_executable 命令,以使生成的产品类型 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上有很多报告的这个问题的实例,或创建新项目和移动文件;

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邮件列表上,但它只报告成功建立一个库,然后出来。 / p>

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 更改为您的公司名称,并将我的名称更改为您的姓名。我发现添加链接目录 \ $ {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.

另外,Xcode似乎没有正确添加资源,当你使用CMake构建项目,所以我添加这一块到 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构建一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:41