Monday, December 30, 2013

YCM with Clang on Windows

YCM, short for YouCompleteMe, is a fast, as-you-type, fuzzy-search code completion engine for Vim. If built with Clang (v3.2+) support, then it can provide
petty code completion for C/C++ code. On linux, we can get or build Clang easily, but on Windows, it not only hard to build, but also not works well.

Listed below are the steps to compile clang on Windows using MinGW64 and make it work with YCM. Pleas note that the Clang built using this methods can only work as parser, instead of a compiler.
That is to say, you can only use it with YCM to provide code completion, and you won’t be able to use it to compile your code. The reason is that the executable file generated by the Clang we build will crash.

1. Download and install MinGW64

Mingw-w64 is a project that provides support to both 64 bit (x64) and 32 bit (x86) GCC on windows.
You could download the MinGW-w64 package using the link below. You could refer to the reason why we use this particular version on Qt.org.

2. Download and install MSYS.

MSYS is a collection of GNU utilities on Windows.

3. Install SVN

Please download and install SVN. Please make sure svn.exe is on on PATH

4. Get LLVM and Clang code

In a MSYS terminal, execute the following commands to get the Clang’s source code. In my case, it is r192871.

cd ~
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm

cd ~/llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang

cd ~/llvm/projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt

5. Modify Clang’s code to let Clang know about our include path.

vi ~/llvm/tools/clang/lib/Frontend/InitHeaderSearch.cpp

Find the following method.

void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {

Within the method, find the following line.

case llvm::Triple::MinGW32:

Add the following lines to right below that position.

// mingw-build C++ include paths
AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "i686-w64-mingw32", "4.8.0");

6. Build LLVM & Clang

In the MSYS terminal, execute the following commands to build Clang.

cd ~
mkdir build
cd build
export CC=gcc
export CXX=g++
../llvm/configure --disable-docs --enable-optimized --disable-assertions --enable-targets=x86,x86_64 --prefix=/mingw
make
make install

7. Install Vim and YCM.

8. Build YCM core

# cd <your YCM folder>
mkdir build && cd build
cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=/mingw -DTEMP=/mingw/bin/clang.dll . ../cpp
make ycm_core
cp ycm/ycm_core.pyd ../python/ycm_core.dll

That’s it. Enjoy YCM on Windows.

For more information, please refer to the following links.

No comments:

Post a Comment