Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

architecture - Building a x86 application with CMake, Ninja and Clang on x64 Windows

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

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
)

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:MeineProgrammeLLVMinclang-cl.exe" -DCMAKE_LINKER:PATH="C:MeineProgrammeLLVMinlld-link.exe"

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

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.

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:MeineProgrammeVisual_Studio2017BuildToolsVCToolsMSVC14.11.25503libx86libcmt.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 CMakeFilescmTC_e2ed5.direstCXXCompiler.cxx.obj

[2/2] Linking CXX executable cmTC_e2ed5.exe

FAILED: cmTC_e2ed5.exe 

cmd.exe /C "cd . && C:MeineProgrammeCMakeincmake.exe -E vs_link_exe --intdir=CMakeFilescmTC_e2ed5.dir --manifests  -- C:MeineProgrammeLLVMinlld-link.exe /nologo CMakeFilescmTC_e2ed5.direstCXXCompiler.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:MeineProgrammeLLVMinlld-link.exe /nologo CMakeFilescmTC_e2ed5.direstCXXCompiler.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:CMakeFilescmTC_e2ed5.dir/intermediate.manifest CMakeFilescmTC_e2ed5.dir/manifest.res" failed (exit code 1) with the following output:
C:MeineProgrammeLLVMinlld-link.exe: warning: <root>: undefined symbol: mainCRTStartup

error: link failed

ninja: build stopped: subcommand failed.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

CMake Issue #16259: "Ninja generator fails with clang-cl when mixing 64/32 bit clang and Visual Studio Environment" suggests utilizing environment variables:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...