Similar questions have been asked before, but I've run into a problem during linking that I haven't seen addressed so I'm making a new question here.
My goal is to compile some C++ code on the command line on Windows using cl-clang
as my compiler, and I want to use ninja as the CMake generator. I'm currently trying to compile a simple hello-world program. My directory looks like this:
.
├── CMakeLists.txt
├── setup.bat
└── src
└── main.cpp
To make setup simpler, I configure cmake using the following setup.bat
file:
@ECHO OFF
if not exist "build\" mkdir build\
cd build
call "C:\Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Auxiliary/Build/vcvarsall.bat" x86
set CC=clang-cl.exe
set CXX=clang-cl.exe
cmake -G Ninja ..
And this CMakeLists.txt
:
cmake_minimum_required(VERSION 3.17.0)
project(ClangNinja)
enable_language(C CXX)
add_executable(foo_bar src/main.cpp)
The cmake setup seems to work fine, with the following output:
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.8.3
** Copyright (c) 2020 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
-- The C compiler identification is Clang 10.0.0 with MSVC-like command-line
-- The CXX compiler identification is Clang 10.0.0 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/clang-cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/clang-cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/dev/c++/cmake/clang_cl_ninja/build
However, when I try to build my project I get the following output, with some linkage errors:
[2/2] Linking CXX executable foo_bar.exe
FAILED: foo_bar.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\foo_bar.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\mt.exe --manifests -- C:\PRO
GRA~2\MICROS~3\2019\PROFES~1\VC\Tools\Llvm\bin\lld-link.exe /nologo CMakeFiles\foo_bar.dir\src\main.cpp.obj /out:foo_bar.exe /implib:foo_bar.lib /pdb:foo_bar.pdb /version:0.0 /machine:X86 /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:\PROGRA~2\MICROS~3\2019\PROFES~1\VC\Tools\Llvm\bin\lld-link.exe /nologo CMakeFiles\foo_bar.dir\src\main.cpp.obj /out:foo_bar.exe /implib:foo_bar.lib /pdb:foo_bar.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /s
ubsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\foo_bar.dir/intermediate.manifest CMakeFiles\foo_bar.dir/manifest.res
" failed (exit code 1) with the following output:
lld-link: error: could not open 'kernel32.lib': no such file or directory
lld-link: error: could not open 'user32.lib': no such file or directory
lld-link: error: could not open 'gdi32.lib': no such file or directory
lld-link: error: could not open 'winspool.lib': no such file or directory
lld-link: error: could not open 'shell32.lib': no such file or directory
lld-link: error: could not open 'ole32.lib': no such file or directory
lld-link: error: could not open 'oleaut32.lib': no such file or directory
lld-link: error: could not open 'uuid.lib': no such file or directory
lld-link: error: could not open 'comdlg32.lib': no such file or directory
lld-link: error: could not open 'advapi32.lib': no such file or directory
lld-link: error: could not open 'msvcrtd.lib': no such file or directory
lld-link: error: could not open 'oldnames.lib': no such file or directory
lld-link: error: could not open 'msvcprtd.lib': no such file or directory
ninja: build stopped: subcommand failed.
As far as I can tell, it's using all the correct tools here, so I'm assuming that vcvarsall.bat
worked as intended, since CMakeCache.txt contains the following:
CMAKE_C_COMPILER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/clang-cl.exe
CMAKE_CXX_COMPILER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/clang-cl.exe
CMAKE_LINKER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/lld-link.exe
What's causing the error messages in the linkage step?