如何使用cmake从针对Windows的Linux交叉编译?

时间:2018-12-16 07:45:06

标签: c++ cmake clang cross-compiling clang++

我目前正在尝试使用Windows Subsystem的Linux子系统中的cmake跨目标Windows进行编译。我正在从Linux编译,因为我希望能够支持多个平台。

我不确定如何使它工作,并且不喜欢编译器。

请参阅以下代码,并为我当前的尝试构建输出。

CMakeLists.txt file

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"
)

win.toolchain.cmake file

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 output

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".

0 个答案:

没有答案