CMake 3.15.0-rc1 - Building with Clang and Ninja on Windows - LNK1104: Cannot open file 'm.lib'
Hi folks,
being curious about the Windows clang support which is announced for the 3.15 version I downloaded the rc1 to give it a first try to create a Ninja build file. As a proof of concept I used the tutorial project (https://cmake.org/cmake-tutorial/#s1). So files used are just those from the Step 1:
- CMakeLists.txt
- tutorial.c
- tutorialconfig.h.in
Unfortunately this did not build and ran into a linker error (see below).
As I have a VS2019 installed I used the define command to make CMake use clang/clang++. Output is as follows:
E:\Coding\CMake\Tutorial\Step1\clang>cmake -DCMAKE_C_COMPILER:PATH="C:\Program Files\LLVM\bin\clang.exe" -DCMAKE_CXX_COMPILER:PATH="C:\Program Files\LLVM\bin\clang++.exe" -DCMAKE_RC_COMPILER:PATH="C:Program Files\LLVM\bin\llvm-rc.exe" -G Ninja ..
-- The C compiler identification is Clang 8.0.0 with GNU-like command-line
-- The CXX compiler identification is Clang 8.0.0 with GNU-like command-line
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang.exe
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang++.exe
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: E:/Coding/CMake/Tutorial/Step1/clang
E:\Coding\CMake\Tutorial\Step1\clang>ninja
[2/2] Linking C executable Tutorial.exe
FAILED: Tutorial.exe
cmd.exe /C "cd . && C:\PROGRA~1\LLVM\bin\clang.exe -nostartfiles -nostdlib -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd CMakeFiles/Tutorial.dir/tutorial.c.obj -o Tutorial.exe -Xlinker /implib:Tutorial.lib -Xlinker /pdb:Tutorial.pdb -Xlinker /version:0.0 -lm.lib -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames && cd ."
LINK : fatal error LNK1104: Cannot open file 'm.lib'
clang: error: linker command failed with exit code 1104 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I assume CMake adds the "m.lib" due to the math include but I am not sure if it is needed here. Looking into the build.ninja I find it in LINK_LIBRARIES:
...
build Tutorial.exe: C_EXECUTABLE_LINKER__Tutorial CMakeFiles/Tutorial.dir/tutorial.c.obj
FLAGS = -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd
LINK_LIBRARIES = -lm.lib -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames
OBJECT_DIR = CMakeFiles\Tutorial.dir
...
When removing -lm.lib
from the build file, ninja is able to build the executable and I can run it.
As I like to use clang without Visual Studio tools on Windows to build C/C++ it would be great if you could make this setup (CMake, LLVM, Ninja [or other]) work smoothly.
Best regards, Felix