记录在Windows下使用VS2017 + Clang成功编译ccls

记录在Windows下使用VS2017成功编译ccls,有兴趣的可以参考一下。

环境:

Windows10  1803 64位
VS2017 15.9.4

安装步骤

第一步:安装VS2017最新版,安装时勾选上CMake

第二步:编译clang

打开 适用于VS2017的x64本机工具命令提示这个cmd

// 下载源码
git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm/llvm.git
git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm/clang.git llvm/tools/clang
// 进入目录
cd llvm

// 生成
cmake -H. -BRelease -G Ninja -DCMAKE_BUILD_TYPE=Release  -DCMAKE_CXX_COMPILER=cl -DCMAKE_C_COMPILER=cl
// 编译
ninja -C Release 

第三步:编译ccls

// 下载代码
git clone  --recursive https://github.com/MaskRay/ccls
// 生成
cmake -H. -BRelease -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang-cl -DSYSTEM_CLANG=On -DCMAKE_PREFIX_PATH="D:/Code/CCPro/llvm/Release;D:/Code/CCPro/llvm/Release/tools/clang;D:/Code/CCPro/llvm;D:/Code/CCPro/llvm/tools/clang"
// 构建
ninja -C Release

-DCMAKE_PREFIX_PATH 对照自己的路径修改。

5 个赞

git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm/clang.git llvm/tools

llvm/tools/clang?

有沒有cmake選項簡化一下set CC=cl set CXX=cl?

ninja -C Release clang

這個可能不夠。需要ninja -C Release clangFormat clangFrontendTool clangIndex clangTooling(這四個clang*及它們的依賴覆蓋的ccls的clang組件依賴)

不好意思,这里写错了,改过来了。

这里我是看的官方文档,https://clang.llvm.org/get_started.html ,可以用-DCMAKE_CXX_COMPILER=cl -DCMAKE_C_COMPILER=cl直接指定

改成编译全部了。

有没有用wsl编译陈宫的例子?

编译的时候电脑卡死几个小时,如何避免这种情况

加內存。开 swap。削 jobs 参数。

公司的电脑。请教一下怎么削jobs参数

ninjia -j1 -C Release

OK,谢谢,很管用

改成编译全部了。

試一下:

cmake -H. -BRelease -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DLLVM_TARGETS_TO_BUILD=X86
ninja -C Release clangFormat clangFrontendTool clangIndex clangTooling clang clang-cl
  • -DBUILD_SHARED_LIBS=ON: 預設靜態連接,.exe會佔大量磁碟空間
  • -DLLVM_TARGETS_TO_BUILD=X86:請測試下這樣是不是依然可以cross-compiler aarch64 arm,我猜測可以:

請把/dev/null替換成Windows下用的設備或一空.cc: clang --target=arm-linux-gnueabi -march=armv7-a -x c++ /dev/null -E -dM

手頭沒有llvm構建環境,你看下是否存在clang-cl這個build target

ninjia -j1 -C Release

這樣會很慢,可以嘗試 -DLLVM_PARALLEL_LINK_JOBS=1,一般連接會佔大量記憶體,編譯不會。

使用Microsoft toolchains时不能生成动态库,使用Mingw可能可以。Building LLVM with CMake — LLVM 15.0.0git documentation

显示下面的内容,完整版:Ubuntu Pastebin

没有clang-cl这个target,但是编译clang会同时编译出clang-cl

https://github.com/MaskRay/ccls/wiki/Build/_compare/59b97bd87ab3b10e314faa219ec0540ef0464e8a...4c5121ab55a7b48701fbc950d6561e2e55aeb63d 這個你看怎麼樣

感觉还是msys2下编译省事,也不用装2017

能够正确编译。

我想刪掉-DSYSTEM_CLANG=,在Windows仍然能用嗎?

git fetch origin pull/221/head:pull221 && git checkout pull221

用vs编译的clang不需要lib前缀,把FindClang.cmake中的lib前缀去掉才能找到Clang::Clang,去掉后就能够编译了

cmake/FindClang.cmake用於Windows要改成什麼樣?在 https://github.com/MaskRay/ccls/pull/221 之後弄壞了?

macro(_Clang_find_library VAR NAME)
  # Windows needs lib prefix
  if (CLANG_ROOT)
    find_library(${VAR} NAMES ${NAME} lib${NAME}
                 NO_DEFAULT_PATH PATHS ${CLANG_ROOT} PATH_SUFFIXES lib)
  else()
    find_library(${VAR} NAMES ${NAME} lib${NAME})
  endif()
endmacro()

今天重新编译了下,没有问题。

这个是咋个回事

D:\MaskRay\llvm>cmake -H. -BRelease -G Ninja -DCMAKE_BUILD_TYPE=Release  -DCMAKE_CXX_COMPILER=cl -DCMAKE_C_COMPILER=cl
-- The C compiler identification is MSVC 19.12.25835.0
-- The CXX compiler identification is MSVC 19.12.25835.0
-- The ASM compiler identification is MSVC
-- Found assembler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx64/x64/cl.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.14/Modules/CMakeTestCCompiler.cmake:60 (message):
  The C compiler

    "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: D:/MaskRay/llvm/Release/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/Neovim/bin/ninja.exe cmTC_c9eaf
    [1/2] Building C object CMakeFiles\cmTC_c9eaf.dir\testCCompiler.c.obj
    [2/2] Linking C executable cmTC_c9eaf.exe
    FAILED: cmTC_c9eaf.exe
    cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_c9eaf.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100162~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100162~1.0\x64\mt.exe --manifests  -- D:\Program\mingw64\bin\ld.exe /nologo CMakeFiles\cmTC_c9eaf.dir\testCCompiler.c.obj  /out:cmTC_c9eaf.exe /implib:cmTC_c9eaf.lib /pdb:cmTC_c9eaf.pdb /version:0.0  /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 "D:\Program\mingw64\bin\ld.exe /nologo CMakeFiles\cmTC_c9eaf.dir\testCCompiler.c.obj /out:cmTC_c9eaf.exe /implib:cmTC_c9eaf.lib /pdb:cmTC_c9eaf.pdb /version:0.0 /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:CMakeFiles\cmTC_c9eaf.dir/intermediate.manifest CMakeFiles\cmTC_c9eaf.dir/manifest.res" failed (exit code 0) with the following output:
    系统找不到指定的文件。
    ninja: build stopped: subcommand failed.




  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:43 (project)


-- Configuring incomplete, errors occurred!
See also "D:/MaskRay/llvm/Release/CMakeFiles/CMakeOutput.log".
See also "D:/MaskRay/llvm/Release/CMakeFiles/CMakeError.log".

D:\MaskRay\llvm>cmake -H. -BRelease -G Ninja -DCMAKE_BUILD_TYPE=Release  -DCMAKE_CXX_COMPILER=cl -DCMAKE_C_COMPILER=cl
-- The C compiler identification is MSVC 19.12.25835.0
-- The CXX compiler identification is MSVC 19.12.25835.0
-- The ASM compiler identification is MSVC
-- Found assembler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx64/x64/cl.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.14/Modules/CMakeTestCCompiler.cmake:60 (message):
  The C compiler

    "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: D:/MaskRay/llvm/Release/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/Neovim/bin/ninja.exe cmTC_8d0f2
    [1/2] Building C object CMakeFiles\cmTC_8d0f2.dir\testCCompiler.c.obj
    [2/2] Linking C executable cmTC_8d0f2.exe
    FAILED: cmTC_8d0f2.exe
    cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_8d0f2.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100162~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100162~1.0\x64\mt.exe --manifests  -- D:\Program\mingw64\bin\ld.exe /nologo CMakeFiles\cmTC_8d0f2.dir\testCCompiler.c.obj  /out:cmTC_8d0f2.exe /implib:cmTC_8d0f2.lib /pdb:cmTC_8d0f2.pdb /version:0.0  /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 "D:\Program\mingw64\bin\ld.exe /nologo CMakeFiles\cmTC_8d0f2.dir\testCCompiler.c.obj /out:cmTC_8d0f2.exe /implib:cmTC_8d0f2.lib /pdb:cmTC_8d0f2.pdb /version:0.0 /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:CMakeFiles\cmTC_8d0f2.dir/intermediate.manifest CMakeFiles\cmTC_8d0f2.dir/manifest.res" failed (exit code 1) with the following output:
    D:\Program\mingw64\bin\ld.exe: cannot find /nologo: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find /out:cmTC_8d0f2.exe: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find /implib:cmTC_8d0f2.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find /pdb:cmTC_8d0f2.pdb: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find /version:0.0: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find /debug: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find /INCREMENTAL: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find /subsystem:console: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find kernel32.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find user32.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find gdi32.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find winspool.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find shell32.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find ole32.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find oleaut32.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find uuid.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find comdlg32.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find advapi32.lib: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find /MANIFEST: No such file or directory
    D:\Program\mingw64\bin\ld.exe: cannot find /MANIFESTFILE:CMakeFiles\cmTC_8d0f2.dir/intermediate.manifest: Invalid argument
    CMakeFiles\cmTC_8d0f2.dir/manifest.res: file not recognized: File format not recognized
    ninja: build stopped: subcommand failed.




  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:43 (project)


-- Configuring incomplete, errors occurred!
See also "D:/MaskRay/llvm/Release/CMakeFiles/CMakeOutput.log".
See also "D:/MaskRay/llvm/Release/CMakeFiles/CMakeError.log".

我也遇到这种情况,不知道怎么搞