CMake交叉编译中WIN32和UNIX不改变
我做了一个测试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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 CMake 遇到第一个
project()
命令后,您不应该编写CMAKE_SYSTEM_NAME
。在第一个project
命令中,选择了编译器和目标系统,以后您无法更改其效果。正如您所看到的,您可以覆盖 CMAKE_SYSTEM_NAME 的值,但这样做的唯一效果是您和可能调用的任何外部 cmake 逻辑(例如使用 find_package 时执行的脚本) code>) 看到一个错误的值(即与编译器的选择不匹配)。对于设置此类信息,您的
CMakeLists.txt
文件是错误的位置。此类信息属于 工具链文件 旁边您选择的编译器实际上会为目标平台生成二进制文件:toolchain-windows.cmake
使用类似
注意: 的内容配置项目 您的 Linux 上的默认编译器几乎肯定无法针对 Windows 进行交叉编译目标。
You're not supposed to write
CMAKE_SYSTEM_NAME
after the firstproject()
command is encountered by CMake. At the firstproject
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 ofCMAKE_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 usingfind_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
Configure the project using something like
Note: The default compiler on your linux almost certainly won't be able cross compile for windows targets.