Building LLVM on Windows using Clang and Ninja fails
Hello, i am trying to build LLVM on Windows (11) using Clang and Ninja. I followed the documentation and the readme on the GitHub repo, and used the following commands:
git clone --config core.autocrlf=false https://github.com/llvm/llvm-project.git
cd llvm-project
cmake -S llvm -B build -G "Ninja" -DCMAKE_BUILD_TYPE=Release
cmake --build build
Both Ninja and Clang are the latest stable release. Since the log itself is incredibly long, here are the parts i think are relevant:
[4/363] Linking CXX shared library bin\LTO.dll
FAILED: bin/LTO.dll lib/LTO.lib
cmd.exe /C "cd . && C:\PROGRA~1\LLVM\bin\CLANG_~1.EXE -fuse-ld=lld-link -nostartfiles -nostdlib -Werror=date-time -Werror=unguarded-availability-new -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -ffunction-sections -fdata-sections -O3 -DNDEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrt -Wl,--gc-sections C:/Users/lexika/Desktop/llvm/llvm-project/build/tools/lto/LTO.def -shared -o bin\LTO.dll -Xlinker /MANIFEST:EMBED -Xlinker /implib:lib\LTO.lib -Xlinker /pdb:bin\LTO.pdb -Xlinker /version:0.0 @CMakeFiles\LTO.rsp && cd ."
lld-link: warning: ignoring unknown argument '--gc-sections'
lld-link: error: C:/Users/lexika/Desktop/llvm/llvm-project/build/tools/lto/LTO.def: unknown file type
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
If i should post the full log (or any other info) just tell me, i'd attach it to this issue if someone wants it.
(Side note: Building with MSVC works)
Any help is appreciated :)
Hi, from my experience, the best way to compile it is using visual studio's x64 native tools command prompt. you can try the following things:
- if you're using
clang, make sure to build underx64 native tools command prompt - otherwise just use
clcompiler underx64 native tools command prompt
I will try that now, thank you!
Let us know if that works; if so, we should update the documentation to recommend using the vs shell.
While it did not error this time, it unfortunately froze in the building process. I waited about a hour before terminating the process since i figured it wouldnt continue. Also, when using the vs shell, even when using ninja as a build system, it somehow forces msvc as the compiler, so this kinda defeats the whole point of compiling with clang.
try build with ninja -j 1? make sure you have enough memory
also try using cl.exe only
try build with ninja -j 1? make sure you have enough memory
note that will make the build take a very long time, but it will isolate a particular compile command that hangs.
Also, when using the vs shell, even when using ninja as a build system, it somehow forces msvc as the compiler
I suspect you would need to rerun cmake, and give it explicit paths to the clang you want to use as the build compiler. (Delete CMakeCache.txt before you rerun cmake.)
I ran into the same problem, and it occurs because the .def file should be passed to the linker. When using Visual Studio, the CMake rules in AddLLVM.cmake add the /DEF flag, which MSVC handles properly, but when using clang, we need to not only add /DEF, but also the -Wl, prefix, so that the /DEF flag is forwarded to the linker and not interpreted by clang. The fix is here: https://reviews.llvm.org/D134165.
The fix from @ashay worked!
@lexika979: Fix was not merged yet.
I think when you switch the compiler to clang-cl instead of clang and running the build commands inside Developer PowerShell for VS 2022+ would help.
There may be uninstalled dependencies related to "C++ ATL for latest v143 build tools (x86 & x64)", make sure to install those from the VS Build Tools.
Optional dependencies like zlib are available through vcpkg.