windows - 在 x64 Windows 上使用 CMake、Ninja 和 Clang 构建 x86 应用程序

标签 windows architecture cmake clang ninja

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

我将 CMake、Ninja、clang-cl、lld-link 和 VS Build Tools 2017 与以下 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 初始化构建环境并使用

调用 cmake
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 构建文件并生成功能可执行文件。

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

CMake错误日志内容如下

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 传递给链接器。

我应该对 CMake 调用或 CMakeLists 进行哪些修改才能解决此问题?

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

最佳答案

CMake Issue #16259: "Ninja generator fails with clang-cl when mixing 64/32 bit clang and Visual Studio Environment"建议使用环境变量:

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

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

关于windows - 在 x64 Windows 上使用 CMake、Ninja 和 Clang 构建 x86 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47581784/

相关文章:

architecture - Servicestack-架构和复用POCO

c++ - 当自定义库名称与系统库名称相同时,CMake出现循环依赖错误

c++ - 使用 CMake 构建 PubNub C++

c++ - CMake:提取 tar 会出现错误,但没有消息

python - python中跨平台控制台应用程序的类curses库

c - 以编程方式提前确定文件 "size on disk"

windows - 从 Delphi 以编程方式检查数字签名

windows - 书中的 FFI 示例在 Windows 下找不到 -lanneclib

amazon-web-services - Lake Formation 治理表底层格式/技术

architecture - 在多层架构中通过层传递业务实体