C++ 使用 CMake 为 iOS 编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12630970/
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
Compiling for iOS with CMake
提问by Tal Zion
I've compiled a C++ static library by using CMake as my building tool and I want to link it to my iOS app.
I created a simple 'Empty' application in Xcode and linked my library called libengine.a to it.
I tried to compile my iOS project and the linker gave me this warning:
我已经使用 CMake 作为我的构建工具编译了一个 C++ 静态库,我想将它链接到我的 iOS 应用程序。
我在 Xcode 中创建了一个简单的“空”应用程序,并将名为 libengine.a 的库链接到它。
我试图编译我的 iOS 项目,链接器给了我这个警告:
ignoring file /Users/.../build/engine/libengine.a,
file was built for archive which is not the architecture being linked (i386):
/Users/.../build/engine/libengine.a
As I understand it, I need to compile my library for ARM processors. The problem is I don't know how.
I think CMake really lacks good tutorials.
Anyways, my CMake scripts are attached below.
Any help would be greatly appreciated.
Thanks, Tal.
Here is my main CMake script:
据我了解,我需要为 ARM 处理器编译我的库。问题是我不知道怎么做。
我认为 CMake 真的缺乏好的教程。
无论如何,我的 CMake 脚本附在下面。
任何帮助将不胜感激。
谢谢,塔尔。
这是我的主要 CMake 脚本:
cmake_minimum_required(VERSION 2.8)
project(movie-night)
if (DEFINED PLATFORM)
include(toolchains/ios.cmake)
endif()
add_definitions(-Wall)
set(DEBUG)
if (DEFINED DEBUG)
add_definitions(-g)
endif()
if (DEFINED RELEASE)
add_definitions(-O3)
endif()
add_subdirectory(engine)
add_subdirectory(ui)
add_subdirectory(test)
Here is my toolchains/ios.cmake file:
这是我的工具链/ios.cmake 文件:
set(CMAKE_SYSTEM_NAME Darwin)
set(CMAKE_SYSTEM_PROCESSOR arm)
采纳答案by dreamzor
Just use this toolchain file: http://code.google.com/p/ios-cmake/and use it as
只需使用此工具链文件:http: //code.google.com/p/ios-cmake/并将其用作
cmake -DCMAKE_TOOLCHAIN_FILE=path_to_your_toolchain_file
Then, in CMakeLists.txt
:
然后,在CMakeLists.txt
:
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch armv7")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch armv7")
回答by russes
There is a second version of iOS.cmake located at:
iOS.cmake 的第二个版本位于:
https://ceres-solver.googlesource.com
https://ceres-solver.googlesource.com
Note: You may find that both versions of iOS.cmake will only build x86 versions of your project. CMake now sets CMAKE_OSX_SYSROOT to the (latest) Mac OS X SDK available on your system. You can fix this by modifying your copy of iOS.cmake to always set CMAKE_OSX_SYSROOT. You can do this by commenting out a couple of lines your copy of iOS.cmake:
注意:您可能会发现 iOS.cmake 的两个版本都只会构建您的项目的 x86 版本。CMake 现在将 CMAKE_OSX_SYSROOT 设置为系统上可用的(最新)Mac OS X SDK。您可以通过修改您的 iOS.cmake 副本以始终设置 CMAKE_OSX_SYSROOT 来解决此问题。您可以通过注释掉 iOS.cmake 副本中的几行来做到这一点:
# -- Under CMake 3.4.2, CMAKE_OSX_SYSROOT is automatically defined to point to the latest Mac OS X SDK. --
# -- So, the iOS SDK is never found. Grab the correct CMAKE_OS_SYSROOT and ignore any prior setting. --
# If user did not specify the SDK root to use, then query xcodebuild for it.
# if (NOT CMAKE_OSX_SYSROOT)
execute_process(COMMAND xcodebuild -version -sdk ${XCODE_IOS_PLATFORM} Path
OUTPUT_VARIABLE CMAKE_OSX_SYSROOT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
message (STATUS "Using SDK: ${CMAKE_OSX_SYSROOT} for platform: ${IOS_PLATFORM}")
message (STATUS "be sure the previous line points to the correct SDK")
# endif ( )
回答by TomTasche
I've been using an updated version of the iOS CMake toolchain for quite a while: https://github.com/leetal/ios-cmake
我一直在使用 iOS CMake 工具链的更新版本:https: //github.com/leetal/ios-cmake
This creates an Xcode project which you can drag into an existing iOS project if necessary.
这将创建一个 Xcode 项目,如有必要,您可以将其拖到现有的 iOS 项目中。
I wrote a blog post with more details: https://blog.tomtasche.at/2019/05/how-to-include-cmake-project-in-xcode.html
我写了一篇包含更多详细信息的博客文章:https: //blog.tomtasche.at/2019/05/how-to-include-cmake-project-in-xcode.html
回答by Vlad
By following cmake-toolchainsdocumentation I did like below:
通过遵循cmake-toolchains文档,我喜欢以下内容:
cmake -G Xcode -B build \
-DCMAKE_SYSTEM_NAME=iOS \
-DCMAKE_Swift_COMPILER_FORCED=true \
-DCMAKE_OSX_DEPLOYMENT_TARGET=11.0
Note:That assignment CMAKE_OSX_DEPLOYMENT_TARGET=11.0
is not a mistake when targeting iOS.
注意:CMAKE_OSX_DEPLOYMENT_TARGET=11.0
当针对 iOS 时,该分配不是错误的。