-
Notifications
You must be signed in to change notification settings - Fork 310
Home
Welcome to the clang_complete wiki!
If you want to build clang yourself, see http://clang.llvm.org/get_started.html. Otherwise see below for already built binaries of clang for easier installation.
sudo apt-get install clang
emerge clang
sudo port install clang
On windows building clang is usually done using mingw or with msvc. Autocompletion works with both compilers, but overall clang itself works better with mingw than with vc++, although both are currently unable to completely link a full program. This is being investigated, but at the moment vc++ is unable to compile but can autocomplete, and mingw can compile & autocomplete but cannot link.
Follow the manual installation and add those lines to your .vimrc:
" The quotes at the beggining of clang_exec and at the end of clang_user_options are important, don't remove them
" They basically trick vim into thinking clang executed fine, because the msvc build autocompletes correctly but fails to compile.
" Don't forget to put paths with spaces in quotes other wise vim won't be able to execute the command
let g:clang_exec = '"C:\path\to\clang.exe'
let g:clang_user_options = '2> NUL || exit 0"'
Alternatively, you can download a prebuilt msvc 2008 binary to avoid going through the build process. If you use another compiler, or for some reasons you want to make it point to other system headers you can modify g:clang_user_options like this:
let g:clang_user_options = '-IC:\foo\bar "-IC:\path with spaces\keke" 2> NUL || exit 0"'
It is possible to build clang using MinGW and get a decent performance (see the benchmarks: https://github.com/Rip-Rip/clang_complete/issues/#issue/4/comment/566341). Following is a description of the method.
The instructions to build clang are based on those found in http://lyuts.net/blog/2010/06/llvm-clang-mingw.
- Get MinGW here - the automated installer. Install it to the default location.
- Get CMake here - the binary win32 installer. I got version 2.8. Install it. Again, do not change the defaults.
- Get LLVM and Clang source code here. I got version 2.8.
- Extract llvm sources. Let E:\LLVM be our working directory. After this step we are supposed to have llvm sources in E:\LLVM\llvm-2.8.
- Extract clang sources to E:\LLVM\llvm-2.8\tools. After this step we are supposed to have clang sources in E:\LLVM\llvm-2.8\tools\clang-2.8. However, this is not how it should be. Rename clang-2.8 to clang.
- Go to E:\LLVM\llvm-2.8. Create a build directory, let it be build.
- Open a command prompt (Run cmd.exe). Change the current directory to E:\LLVM\llvm-2.8\build.
- Run (this is a long line... do not forget to include the two dots ("..") in the end of the line):
E:\LLVM\llvm-2.8\build>"C:\Program Files (x86)\CMake 2.8\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=X86 -DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc --disable-shared --enable-static" -DCMAKE_SHARED_LINKER_FLAGS="-static-libstdc++ -static-libgcc --disable-shared --enable-static" -G "MinGW Makefiles" ..
if you are on Windows 7 use this line (on Windows 7 CMake is installed to C:\Program Files (x86)\CMake 2.8\bin):
E:\LLVM\llvm-2.8\build>"C:\Program Files (x86)\CMake 2.8\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=X86 -DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc --disable-shared --enable-static" -DCMAKE_SHARED_LINKER_FLAGS="-static-libstdc++ -static-libgcc --disable-shared --enable-static" -G "MinGW Makefiles" ..
- After the configuring is done run C:\MinGW\bin\mingw32-make This will take some time to get llvm and clang built.
Instead of steps 3-5 you may get the sources using subversion (see http://clang.llvm.org/get_started.html, the section for unix-like systems).
A little explanation of the long cmake command line is due:
- First, we would like a release build. A release build is built using most available optimizations and is of course much faster than a debug build.
- Second, we would like to build a statically linked executable. By default MinGW builds an executable which depends on some MinGW dlls. This makes the start up time of the program much longer, since Windows has to look for all these dlls each time we start the program. By building it statically we make it start much faster. Since we are going to run clang every time we try to complete something, start-up time is of highly importance.
As written above, Clang chokes on the standard Windows header files. Luckily the good folks at MinGW wrote compatible header files which Clang is ok with.
Look at the following contrived example code. "Windows.h" is only included for illustrative purposes, it is not really needed here. The file name is 'test.cpp'.
#include <windows.h>
#include <vector>
#include <string>
#include <boost/shared_ptr.hpp>
int main()
{
typedef boost::shared_ptr<std::string> stringPtr;
std::vector<stringPtr> stringVector;
stringVector.push_back( stringPtr(new std::string) );
stringVector[0]->
return 0;
}
The following code is the contents of "pch.h":
#include <windows.h>
#include <vector>
#include <string>
#include <boost/shared_ptr.hpp>
I am using the rxvt-native (without X support) bash shell from cygwin for the following examples. Note that the paths on your system might be different, depending on the versions installed etc. Note also that I installed the boost library to E:\boost_1_44_0.
A script to create the pre-compiled-headers file:
$ cat build_clang_pch.bash
clang -fno-exceptions -fgnu-runtime -x c++-header \
pch.h -o pch.h.pch \
-I"E:\LLVM\llvm-2.8\build\lib\clang\\2.8\include" \
-I"C:\MinGW\lib\gcc\mingw32\\4.5.0\include" \
-I"E:\boost_1_44_0" \
-fms-extensions -fmsc-version=1300 \
-D__MSVCRT_VERSION__=0x700 -D_WIN32_WINNT=0x0500 \
-include malloc.h
The following script runs clang without using a pre-compiled headers file.
$ cat test_completion.bash
clang -cc1 -fsyntax-only -fno-caret-diagnostics -fdiagnostics-print-source-range-info \
-code-completion-at=test.cpp:11:22 test.cpp \
-I"E:\LLVM\llvm-2.8\build\lib\clang\\2.8\include" \
-I"C:\MinGW\lib\gcc\mingw32\\4.5.0\include" \
-I"E:\boost_1_44_0" \
-fms-extensions -fmsc-version=1300 -fgnu-runtime \
-D__MSVCRT_VERSION__=0x700 -D_WIN32_WINNT=0x0500 \
-include malloc.h
The following script runs clang using a pre-compiled headers file.
$ cat test_completion_no_pch.bash
clang -cc1 -fsyntax-only -fno-caret-diagnostics -fdiagnostics-print-source-range-info \
-code-completion-at=test.cpp:11:22 test.cpp \
-I"E:\LLVM\llvm-2.8\build\lib\clang\\2.8\include" \
-I"C:\MinGW\lib\gcc\mingw32\\4.5.0\include" \
-I"E:\boost_1_44_0" \
-fms-extensions -fmsc-version=1300 -fgnu-runtime \
-D__MSVCRT_VERSION__=0x700 -D_WIN32_WINNT=0x0500 \
-include-pch pch.h.pch \
-include malloc.h
We used 3 'optimizations' to get clang work as fast as possible:
- Clang was built statically
- Clang was built using 'release' flags rather than debug
- We use a pre-compiled headers file.
Another 'optimization' you might try is normalizing your header files. In this context 'normalizing' means to not include a header file in another header file. Instead include all necessary header files in the cpp file. This is not always possible, but usually using forward declarations can get you a long way.
If you follow these guidelines, Clang will emit no errors and it would not be necessary to filter its output.
As a footnote, add the following to your .vimrc file:
" fix cygwin shell redirection
set shellredir=>\\"%s\\"\\ 2>&1
This is necessary if you run gvim from a cygwin bash shell.