在 x64 Windows 上使用 CMake、Ninja 和 Clang 构建 x86 应用程序 [英] Building a x86 application with CMake, Ninja and Clang on x64 Windows

查看:86
本文介绍了在 x64 Windows 上使用 CMake、Ninja 和 Clang 构建 x86 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 x64 Windows 机器上构建一个 x86 Windows 应用程序.

I want to build an x86 Windows application on my x64 Windows machine.

我使用 CMake、Ninja、clang-cl、lld-link 和 VS Build Tools 2017 以及以下 CMakeLists

I use CMake, Ninja, clang-cl, lld-link and the VS Build Tools 2017 with the following CMakeLists

cmake_minimum_required(VERSION 3.9)
project(Test CXX)

add_library(TestLib STATIC "")
target_include_directories(TestLib
  PUBLIC
TestLib/inc
)
target_sources(TestLib
  PRIVATE
    TestLib/src/Flop.cpp
    TestLib/src/testClass.cpp
)

add_executable(Test "")
target_sources(Test
  PRIVATE
    src/main.cpp
)
target_link_libraries(Test
  TestLib
)

我的设置适用于 x64 应用程序.我用 vcvars64 初始化构建环境并用

My setup works fine for x64 applications. I initialise the build environment with vcvars64 and call cmake with

cmake -G Ninja -DCMAKE_CXX_COMPILER:PATH="C:\MeineProgramme\LLVM\bin\clang-cl.exe" -DCMAKE_LINKER:PATH="C:\MeineProgramme\LLVM\bin\lld-link.exe"

这会产生完美的 ninja 构建文件并生成一个功能性的可执行文件.

This results in perfectly fine ninja build files and produces a functional executable.

如果我想构建一个 x86 应用程序,CMake 无法检测构建环境.我使用 vcvars32vcvarsamd64_x86 初始化构建环境,并使用与上述相同的命令来调用 CMake.这会导致在检测构建环境时出错.

If I want to build an x86 application, CMake failes to detect the build environment. I initialise the build environment with vcvars32 or vcvarsamd64_x86 and use the same command as above to invoke CMake. This results in errors during the detection of the build environment.

CMake 错误日志有以下内容

The CMake error log has the following content

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: C:/MeineProgramme/LLVM/bin/clang-cl.exe 
Build flags: 
Id flags:  

The output was:
1120
LINK : error LNK2001: Nicht aufgelöstes externes Symbol "mainCRTStartup".
C:\MeineProgramme\Visual_Studio\2017\BuildTools\VC\Tools\MSVC\14.11.25503\lib\x86\libcmt.lib : warning LNK4272:Bibliothekcomputertyp "x86" steht in Konflikt mit dem Zielcomputertyp "x64"
CMakeCXXCompilerId.exe : fatal error LNK1120: 1 nicht aufgelöste Externe
clang-cl.exe: error: linker command failed with exit code 1120 (use -v to see invocation)


Determining if the CXX compiler works failed with the following output:
Change Dir: D:/Dateien/Downloads/Test/build/CMakeFiles/CMakeTmp

Run Build Command:"C:/MeineProgramme/Ninja/bin/ninja.exe" "cmTC_e2ed5"
[1/2] Building CXX object CMakeFiles\cmTC_e2ed5.dir\testCXXCompiler.cxx.obj

[2/2] Linking CXX executable cmTC_e2ed5.exe

FAILED: cmTC_e2ed5.exe 

cmd.exe /C "cd . && C:\MeineProgramme\CMake\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_e2ed5.dir --manifests  -- C:\MeineProgramme\LLVM\bin\lld-link.exe /nologo CMakeFiles\cmTC_e2ed5.dir\testCXXCompiler.cxx.obj  /out:cmTC_e2ed5.exe /implib:cmTC_e2ed5.lib /pdb:cmTC_e2ed5.pdb /version:0.0  /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."

LINK Pass 1: command "C:\MeineProgramme\LLVM\bin\lld-link.exe /nologo CMakeFiles\cmTC_e2ed5.dir\testCXXCompiler.cxx.obj /out:cmTC_e2ed5.exe /implib:cmTC_e2ed5.lib /pdb:cmTC_e2ed5.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_e2ed5.dir/intermediate.manifest CMakeFiles\cmTC_e2ed5.dir/manifest.res" failed (exit code 1) with the following output:
C:\MeineProgramme\LLVM\bin\lld-link.exe: warning: <root>: undefined symbol: mainCRTStartup

error: link failed

ninja: build stopped: subcommand failed.

据我所知,问题在于 CMake 将 /machine:x64 传递给链接器.

As far as I understand, the problem is that CMake passes /machine:x64 to the linker.

我应该对 CMake 调用或 CMakeLists 进行哪些修改来解决这个问题?

Which modifications should I make to the CMake invocation or the CMakeLists to solve this problem?

非常感谢任何帮助.谢谢你们:-)

Any help is very much appreciated. Thank you guys :-)

推荐答案

CMake Issue #16259:Ninja 生成器在混合 64/32 位 clang 和 Visual Studio 环境时使用 clang-cl 失败" 建议使用环境变量:

Ninja 生成器要求已经为编译器配置了环境以生成所需的目标.你需要做的

The Ninja generator requires that the environment already be configured for the compiler to produce the desired target. You need to do

>set CC=clang-cl
>set CFLAGS=-m32
>set CXX=clang-cl
>set CXXFLAGS=-m32

告诉 CMake 如何为所需的目标调用编译器.

to tell CMake how to invoke the compiler for the desired target.

这篇关于在 x64 Windows 上使用 CMake、Ninja 和 Clang 构建 x86 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆