我正在尝试使用ios-cmake生成针对iOS的Xcode项目。但是,它找不到Threads。这是一个简单的CMake脚本用于演示:
CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
PROJECT (MyCITest)
SET (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
########################
# EDIT: I've also tried adding the lines below prior to posting this question,
# but there doesn't seem to be any effect.
# (http://stackoverflow.com/questions/8386897)
SET (CMAKE_REQUIRED_INCLUDES ${CMAKE_IOS_SDK_ROOT}/usr ${CMAKE_IOS_SDK_ROOT}/usr/include)
SET (CMAKE_CXX_FLAGS "--sysroot=${CMAKE_IOS_SDK_ROOT} ${CMAKE_CXX_FLAGS}")
SET (CMAKE_C_FLAGS "--sysroot=${CMAKE_IOS_SDK_ROOT} ${CMAKE_C_FLAGS}")
########################
FIND_PACKAGE (ZLIB REQUIRED)
FIND_PACKAGE (LibXml2 REQUIRED)
FIND_PACKAGE (Threads REQUIRED)
从终端运行CMake:
cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/iOS.cmake -GXcode
这是我得到的输出:
-- Toolchain using default iOS SDK: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk
-- Found ZLIB: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/usr/lib/libz.dylib (found version "1.2.5")
-- Found LibXml2: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/usr/lib/libxml2.dylib (found version "2.7.8")
-- Looking for include file pthread.h
-- Looking for include file pthread.h - not found
CMake Error at cmake/modules/FindPackageHandleStandardArgs.cmake:97 (MESSAGE):
Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
cmake/modules/FindPackageHandleStandardArgs.cmake:288 (_FPHSA_FAILURE_MESSAGE)
cmake/modules/FindThreads.cmake:166 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
CMakeLists.txt:8 (FIND_PACKAGE)
-- Configuring incomplete, errors occurred!
我已经三次检查过pthread.h位于/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/usr/include
,此外,它找到了ZLib和LibXML2没有问题,所以我不确定我做错了什么。
> cmake --version
cmake version 2.8.10.2
答案 0 :(得分:9)
这些解决方案似乎都不起作用。我发现唯一一直对我有用的是设置FindThreads.cmake设置的变量。换句话说,在工具链文件中定义以下内容:
Path path = Paths.get("C:\\Users\\jorgesys\\workspaceJava\\myfile.txt");
BasicFileAttributes attr;
try {
attr = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("Creation date: " + attr.creationTime());
//System.out.println("Last access date: " + attr.lastAccessTime());
//System.out.println("Last modified date: " + attr.lastModifiedTime());
} catch (IOException e) {
System.out.println("oops error! " + e.getMessage());
}
答案 1 :(得分:2)
事实证明,iOS工具链目前不支持由TRY_COMPILE
使用的CheckIncludeFiles.cmake
,而FindThreads.cmake
则由TRY_COMPILE
使用。工具链目前设置为使用以下内容跳过set (CMAKE_CXX_COMPILER_WORKS TRUE)
set (CMAKE_C_COMPILER_WORKS TRUE)
:
{{1}}
参考:http://code.google.com/p/ios-cmake/issues/detail?id=1&can=1
答案 2 :(得分:1)
您可以使用工具链中的下一个变量修复try_compile
命令:
# toolchain.cmake
set(CMAKE_MACOSX_BUNDLE YES)
set(CMAKE_OSX_SYSROOT "iphoneos")
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer")
set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.example")
简约的例子:
# CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(Foo)
find_package(Threads REQUIRED)
add_executable(foo foo.cpp)
生成输出:
> cmake -H. -B_builds -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake -GXcode
...
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE
...
答案 3 :(得分:1)
如here所述。
需要使用XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED
而不是下面的CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED
来完成这项工作:
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES "XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED")
set(XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO")
find_package(Threads REQUIRED)
unset(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES)
unset(XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED)
here对问题进行了讨论,并由@alcroito进行了解释。它不使用自定义ios-cmake工具链,而是使用新的native iOS toolchain(CMake 3.14+),但问题是相同的。
使用Xcode生成器时,默认情况下它将尝试对应用程序进行签名。寻找Threads
时,CMake会调用FindThreads.cmake
,这将调用TRY_COMPILE
测试Xcode将尝试签名并失败。
在try_compile
中禁用对FindThreads
的签名(见here):
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES "CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED")
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
find_package(Threads REQUIRED)
unset(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES)
unset(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED)
答案 4 :(得分:0)
提一下,我的项目也遇到了同样的问题。我注释掉了find_package(Threads REQUIRED)
行并生成了Xcode proejct。编译的代码没有错误。也许Xcode可以自动链接到posix线程库。
答案 5 :(得分:0)
对我而言,解决方案是我之前的2个答案的组合。
注释掉禁用try_compile
# Skip the platform compiler checks for cross compiling
set (CMAKE_CXX_COMPILER_WORKS TRUE)
set (CMAKE_C_COMPILER_WORKS TRUE)
然后将其添加到IOS.cmake工具链文件的顶部。
set(MACOSX_BUNDLE_GUI_IDENTIFIER com.example)
set(CMAKE_MACOSX_BUNDLE YES)
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer")
答案 6 :(得分:0)
问题确实是由于在FindThreads.cmake中对try_compile
的调用失败。但是对我来说,仅设置CMAKE_CXX_COMPILER_WORKS
是不够的。相反,我将try_compile
进行的测试类型从尝试构建可执行文件更改为尝试构建静态库,方法是将其放入工具链文件中:
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)