C++ 如何使用cmake从Linux交叉编译到Windows?
我目前正试图在Windows Ubuntu的Linux子系统中使用cmake交叉编译目标Windows。我编译FormLinux是因为我希望能够支持多种平台 我不确定如何使这项工作,并没有首选的编译器 请参阅以下代码和我当前尝试的生成输出C++ 如何使用cmake从Linux交叉编译到Windows?,c++,cmake,clang,cross-compiling,clang++,C++,Cmake,Clang,Cross Compiling,Clang++,我目前正试图在Windows Ubuntu的Linux子系统中使用cmake交叉编译目标Windows。我编译FormLinux是因为我希望能够支持多种平台 我不确定如何使这项工作,并没有首选的编译器 请参阅以下代码和我当前尝试的生成输出 CMakeLists.txt文件 cmake_minimum_required(VERSION 3.7) set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/win.toolchain.cmake) p
CMakeLists.txt文件
cmake_minimum_required(VERSION 3.7)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/win.toolchain.cmake)
project(environment.exe)
find_package(SDL2 REQUIRED)
include_directories(environment.exe ${SDL2_INCLUDE_DIRS})
# get executable src files
file(GLOB_RECURSE environment.exe_src ${CMAKE_CURRENT_SOURCE_DIR}/src/environment/*.cpp)
# add environment.exe executable
add_executable(environment.exe ${environment.exe_src})
target_link_libraries(environment.exe ${SDL2_LIBRARIES})
# set output dir
set_target_properties(
environment.exe
PROPERTIES
# ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/suite/lib"
# LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/suite/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/environment"
)
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR x64)#i686
set(triple x64-pc-win32)
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${triple})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET ${triple})
win.toolchain.cmake文件
cmake_minimum_required(VERSION 3.7)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/win.toolchain.cmake)
project(environment.exe)
find_package(SDL2 REQUIRED)
include_directories(environment.exe ${SDL2_INCLUDE_DIRS})
# get executable src files
file(GLOB_RECURSE environment.exe_src ${CMAKE_CURRENT_SOURCE_DIR}/src/environment/*.cpp)
# add environment.exe executable
add_executable(environment.exe ${environment.exe_src})
target_link_libraries(environment.exe ${SDL2_LIBRARIES})
# set output dir
set_target_properties(
environment.exe
PROPERTIES
# ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/suite/lib"
# LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/suite/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/environment"
)
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR x64)#i686
set(triple x64-pc-win32)
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${triple})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET ${triple})
BASH输出
a@DESKTOP-DCHFQSJ:/mnt/e/d/User/Software Projects/windows-software-environment$ ./build.sh
Checking for make installation.
make is installed.
Checking for cmake installation.
cmake is installed.
Checking for mingw-w64 installation.
mingw-w64 is installed.
Checking for g++-mingw-w64 installation.
g++-mingw-w64 is installed.
Checking for libsdl2-dev installation.
libsdl2-dev is installed.
./build.sh: line 30: cd: ./build: No such file or directory
Running cmake on './build'
-- The C compiler identification is Clang 6.0.0
-- The CXX compiler identification is Clang 6.0.0
-- Check for working C compiler: /usr/bin/clang
-- Check for working C compiler: /usr/bin/clang -- broken
CMake Error at /usr/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 (message):
The C compiler
"/usr/bin/clang"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /mnt/e/d/User/Software Projects/windows-software-environment/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_8a6e7/fast"
/usr/bin/make -f CMakeFiles/cmTC_8a6e7.dir/build.make CMakeFiles/cmTC_8a6e7.dir/build
make[1]: Entering directory '/mnt/e/d/User/Software Projects/windows-software-environment/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_8a6e7.dir/testCCompiler.c.obj
/usr/bin/clang --target=x64-pc-win32 -o CMakeFiles/cmTC_8a6e7.dir/testCCompiler.c.obj -c "/mnt/e/d/User/Software Projects/windows-software-environment/build/CMakeFiles/CMakeTmp/testCCompiler.c"
error: unknown target triple 'x64-pc-windows-msvc19.11.0', please use -triple or -arch
CMakeFiles/cmTC_8a6e7.dir/build.make:65: recipe for target 'CMakeFiles/cmTC_8a6e7.dir/testCCompiler.c.obj' failed
make[1]: *** [CMakeFiles/cmTC_8a6e7.dir/testCCompiler.c.obj] Error 1
make[1]: Leaving directory '/mnt/e/d/User/Software Projects/windows-software-environment/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_8a6e7/fast' failed
make: *** [cmTC_8a6e7/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:19 (project)
-- Configuring incomplete, errors occurred!
See also "/mnt/e/d/User/Software Projects/windows-software-environment/build/CMakeFiles/CMakeOutput.log".
See also "/mnt/e/d/User/Software Projects/windows-software-environment/build/CMakeFiles/CMakeError.log".
看起来您选择了使用
clang
,但没有选择跨编译器版本的clang
。什么是/build.sh
。它似乎可以找到您安装的交叉编译器,但您没有使用它们。您应该首先了解如何设置环境,以及如何手动调用编译器。然后使用正确的信息创建工具链文件。/build.sh
只需检查包,如果不存在,则安装包,然后运行cmake
,然后运行make
。它检查的其他编译器都是以前尝试过的。