CMake交叉编译中WIN32和UNIX不改变

发布于 2025-01-16 23:34:08 字数 1357 浏览 1 评论 0原文

我做了一个测试CMakeLists.txt。我在读取变量之前设置了系统名称和版本(尽管我没有设置编译器):

cmake_minimum_required(VERSION 3.10)

project(test)

include_directories(.)

set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10.0.10240.0)

add_executable(test test.c)

target_link_libraries(test)

message("cmake system name = ${CMAKE_SYSTEM_NAME}")
message("cmake host name = ${CMAKE_HOST_SYSTEM_NAME}")
message("cmake system version = ${CMAKE_SYSTEM_VERSION}")
message("unix = ${UNIX}")
message("win32 = ${WIN32}")

这是“cmake”的输出。在终端上:

-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
cmake system name = Windows
cmake host name = Linux
cmake system version = 10.0.10240.0
unix = 1
win32 = 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/francisco/Documents

CMake 文档说 UNIX 和 WIN32 设置为目标操作系统。在这里,我设置了目标操作系统,但它们仍然设置为主机操作系统。为什么?

I made a test CMakeLists.txt. I set system name and version before reading variables (although I didn't set the compiler):

cmake_minimum_required(VERSION 3.10)

project(test)

include_directories(.)

set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10.0.10240.0)

add_executable(test test.c)

target_link_libraries(test)

message("cmake system name = ${CMAKE_SYSTEM_NAME}")
message("cmake host name = ${CMAKE_HOST_SYSTEM_NAME}")
message("cmake system version = ${CMAKE_SYSTEM_VERSION}")
message("unix = ${UNIX}")
message("win32 = ${WIN32}")

Here is the output from "cmake ." on the terminal:

-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
cmake system name = Windows
cmake host name = Linux
cmake system version = 10.0.10240.0
unix = 1
win32 = 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/francisco/Documents

The CMake documentation says UNIX and WIN32 are set to the target OS. Here I set the target OS and yet they remain set to the host OS. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞取 2025-01-23 23:34:08

在 CMake 遇到第一个 project() 命令后,您不应该编写 CMAKE_SYSTEM_NAME。在第一个 project 命令中,选择了编译器和目标系统,以后您无法更改其效果。正如您所看到的,您可以覆盖 CMAKE_SYSTEM_NAME 的值,但这样做的唯一效果是您和可能调用的任何外部 cmake 逻辑(例如使用 find_package 时执行的脚本) code>) 看到一个错误的值(即与编译器的选择不匹配)。

对于设置此类信息,您的 CMakeLists.txt 文件是错误的位置。此类信息属于 工具链文件 旁边您选择的编译器实际上会为目标平台生成二进制文件:

toolchain-windows.cmake

set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10.0.10240.0)

set(CMAKE_C_COMPILER <command/absolute path to executable to use as C compiler goes here>)
set(CMAKE_CXX_COMPILER <command/absolute path to executable to use as C++ compiler goes here>)

...

使用类似

cmake --toolchain path/to/toolchain-windows.cmake -S path/to/source -B path/to/build/dir

注意: 的内容配置项目 您的 Linux 上的默认编译器几乎肯定无法针对 Windows 进行交叉编译目标。

You're not supposed to write CMAKE_SYSTEM_NAME after the first project() command is encountered by CMake. At the first project command the choices of compiler and target system are made and you cannot change the effects of this later. As you have seen you can overwrite the value of CMAKE_SYSTEM_NAME, but the only effect of doing this are that you and any external cmake logic that may be invoked (e.g. scripts executed when using find_package) see a value that is wrong (i.e. not matching the choice of compiler).

For setting this kind of information your CMakeLists.txt files are the wrong place. This kind of info belongs into a toolchain file alongside your choice of compiler that actually produces binaries for the target platform:

toolchain-windows.cmake

set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10.0.10240.0)

set(CMAKE_C_COMPILER <command/absolute path to executable to use as C compiler goes here>)
set(CMAKE_CXX_COMPILER <command/absolute path to executable to use as C++ compiler goes here>)

...

Configure the project using something like

cmake --toolchain path/to/toolchain-windows.cmake -S path/to/source -B path/to/build/dir

Note: The default compiler on your linux almost certainly won't be able cross compile for windows targets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文