gpt4 book ai didi

macos - 在 CMake 中将 SDK 交叉编译到 OSX

转载 作者:行者123 更新时间:2023-12-05 05:25:05 28 4
gpt4 key购买 nike

我正在使用 CMake 从 Linux 交叉编译到 OSX。为此,我使用了一个工具链文件,因此调用如下:

cmake -G 'Unix Makefiles' -DCMAKE_TOOLCHAIN_FILE=./cmake/toolchains/c.apple.universal.cmake .

工具链文件如下所示:

SET(CMAKE_SYSTEM_NAME Darwin)
SET(CMAKE_SYSTEM_PROCESSOR universal)

# set compilers...
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/c.apple.common.cmake")

还有一个:

INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../Modules/CMakeMacroSetCCache.cmake")

# specify the cross compiler
SET_CCACHE(CMAKE_C_COMPILER i686-apple-darwin10-gcc)
SET_CCACHE(CMAKE_CXX_COMPILER i686-apple-darwin10-g++)
SET(CMAKE_RANLIB i686-apple-darwin10-ranlib CACHE STRING "" FORCE)
SET(CMAKE_LIPO i686-apple-darwin10-lipo CACHE STRING "" FORCE)

SET(OSX104_SDK "/usr/lib/apple/SDKs/MacOSX10.4.sdk")
SET(OSX105_SDK "/usr/lib/apple/SDKs/MacOSX10.5.sdk")

# set SDK
SET(CMAKE_OSX_DEPLOYMENT_TARGET )
IF(EXISTS ${OSX104_SDK})
SET(CMAKE_OSX_SYSROOT ${OSX104_SDK})
ELSEIF(EXISTS ${OSX105_SDK})
SET(CMAKE_OSX_SYSROOT ${OSX105_SDK})
ELSE()
MESSAGE(FATAL_ERROR "No OSX SDK found!")
ENDIF()
MESSAGE(STATUS "Using OSX SDK at ${CMAKE_OSX_SYSROOT}")

SET(CMAKE_PREFIX_PATH ${CMAKE_OSX_SYSROOT})
SET(CMAKE_FIND_ROOT_PATH ${CMAKE_PREFIX_PATH})
SET(BOOST_ROOT ${CMAKE_PREFIX_PATH})

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

到目前为止,这是可行的,但 CMake 在某些时候会以某种方式重置 CMAKE_OSX_SYSROOT。我得到的输出是:

-- Set CMAKE_C_COMPILER to /usr/lib/ccache-lipo/i686-apple-darwin10-gcc
-- Set CMAKE_CXX_COMPILER to /usr/lib/ccache-lipo/i686-apple-darwin10-g++
-- Using OSX SDK at /usr/lib/apple/SDKs/MacOSX10.5.sdk
-- Set CMAKE_C_COMPILER to /usr/lib/ccache-lipo/i686-apple-darwin10-gcc
-- Set CMAKE_CXX_COMPILER to /usr/lib/ccache-lipo/i686-apple-darwin10-g++
-- Using OSX SDK at /usr/lib/apple/SDKs/MacOSX10.5.sdk
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /usr/lib/ccache-lipo/i686-apple-darwin10-gcc
-- Check for working C compiler: /usr/lib/ccache-lipo/i686-apple-darwin10-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Check for working CXX compiler: /usr/lib/ccache-lipo/i686-apple-darwin10-g++
-- Check for working CXX compiler: /usr/lib/ccache-lipo/i686-apple-darwin10-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Used Toolchain definition file '/srv/jenkins/.../cmake/toolchains/c.apple.universal.cmake'
-- Configuring for cross-compiling to Darwin on universal
-- Using platform config cmake/darwin.cmake
-- Checking /Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.B.dylib for possible architectures

最后 3 行来自 CMakeList.txt。使用了正确的工具链文件(这只是一条消息),SYSTEM_NAME 和 SYSTEM_PROCESSOR 设置正确但 OSX SDK 错误。相应的 CMake 代码来自一个宏,如果之前没有设置它,它将设置 CMAKE_OSX_ARCHITECTURES(这里就是这种情况)。该行是:

MESSAGE(STATUS "Checking ${CMAKE_OSX_SYSROOT}/usr/lib/libSystem.B.dylib for possible architectures")

我在这里错误地使用了 CMake 吗?为什么 CMake 重置 OSX_SYSROOT?根据http://www.cmake.org/cmake/help/v3.0/variable/CMAKE_OSX_SYSROOT.html它也应该影响 FIND* 命令,但我需要设置 CMAKE_FIND_ROOT_PATH 才能工作。

很奇怪,它昨天起作用了。清理整个构建目录(我确定我昨天也这样做了)并重新运行 CMake 现在重置 CMAKE_OSX_SYSROOT。

如果这有所不同:在包含结束执行宏之前调用 PROJECT,则显示错误的 sysroot。

最佳答案

我找到的解决方案是将变量设置到缓存中。这样它在 CMake 处理期间“存活”:

SET(CMAKE_OSX_SYSROOT ${CMAKE_OSX_SYSROOT} CACHE PATH "..." FORCE)

关于macos - 在 CMake 中将 SDK 交叉编译到 OSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32414112/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com