llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Building LLVM on Windows using Clang and Ninja fails

Open ghost opened this issue 4 years ago • 11 comments

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

ghost avatar Jun 28 '22 17:06 ghost

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:

  1. if you're using clang, make sure to build under x64 native tools command prompt
  2. otherwise just use cl compiler under x64 native tools command prompt

syheliel avatar Jun 29 '22 02:06 syheliel

I will try that now, thank you!

ghost avatar Jun 29 '22 06:06 ghost

Let us know if that works; if so, we should update the documentation to recommend using the vs shell.

pogo59 avatar Jun 29 '22 12:06 pogo59

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.

ghost avatar Jun 29 '22 12:06 ghost

try build with ninja -j 1? make sure you have enough memory

syheliel avatar Jun 29 '22 13:06 syheliel

also try using cl.exe only

syheliel avatar Jun 29 '22 13:06 syheliel

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

pogo59 avatar Jun 29 '22 13:06 pogo59

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.

ashay avatar Sep 19 '22 07:09 ashay

The fix from @ashay worked!

ghost avatar Sep 21 '22 11:09 ghost

@lexika979: Fix was not merged yet.

EugeneZelenko avatar Sep 21 '22 13:09 EugeneZelenko

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.

bassiounix avatar Jan 30 '25 00:01 bassiounix