# Home

Silex edited this page Sep 26, 2012 · 39 revisions

Welcome to the clang_complete wiki!

## Clang Installation:

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.

### Debian/Ubuntu

sudo apt-get install clang

### Gentoo

emerge clang

### Arch Linux

pacman -S clang

### MacOSX

sudo port install clang

### Windows

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.

#### Building with msvc

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"'

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"'

#### Building with mingw

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.

1. Get MinGW here - the automated installer. Install it to the default location.
2. Get CMake here - the binary win32 installer. I got version 2.8. Install it. Again, do not change the defaults.
3. Get LLVM and Clang source code here. I got version 2.8.
4. 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.
5. 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.
6. Go to E:\LLVM\llvm-2.8. Create a build directory, let it be build.
7. Open a command prompt (Run cmd.exe). Change the current directory to E:\LLVM\llvm-2.8\build.
8. 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\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 using the 64-bit version of Windows, use this line instead (since CMake will be 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" ..
1. After the configuration is done run C:\MinGW\bin\mingw32-make. This will take some time to get LLVM and clang built.
Note: If you are having g++ errors concerning the --disable-shared and --enable-static flags when attempting to make the project, remove these flags from the -DCMAKE_EXE_LINKER_FLAG and -DCMAKE_SHARED_LINKER_FLAGS options in the command from step 8 and run the modified command. Then attempt this step again.

2. In order to install clang, use C:\MinGW\bin\mingw32-make install/strip (that is default for making packages with cmake, but not for normal installations). It strips symbols and hence produces smaller executable.

3. Add the bin folder from the installation directory (which is usually C:\Program Files\LLVM\bin) to your system PATH. At this point, clang_complete should work for C files. But for using STL with C++, an additional step is required. Follow on to step 12.

4. This step is required only if you are using the standard C++ libraries. At the root directory of your C++ project, create a file named .clang_complete. Add the following lines in the file:

-IC:/MinGW/include
-IC:/MinGW/lib
-IC:/MinGW/lib/gcc/mingw32/4.6.2/include/c++
-IC:/MinGW/lib/gcc/mingw32/4.6.2/include/c++/mingw32


This is assuming that you have installed MinGW to C:\MinGW and you are using GCC version 4.6.2. If that is not the case, adjust the paths according to your system.

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.

#### Using Clang

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_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 malloc.h

The following script runs clang 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-pch pch.h.pch \
-include malloc.h

#### Summary

We used 3 'optimizations' to get clang work as fast as possible:

1. Clang was built statically
2. Clang was built using 'release' flags rather than debug
3. 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.

## Using clang library

By default clang_complete use clang executable to complete the code. It is possible to use the clang library which improves the completion speed (look in the documentation of clang_complete for more informations). To make it working you need:

• python installed
• vim with python support (check it with :version)
• in your .vimrc add: let g:clang_use_library = 1

You can check that clang_complete use the library with :let g:clang_use_library (it returns 1 when using the library)

### Troubleshootings

Q: The completion works with the clang executable but when I use the clang library I have the following error message: 'User defined completion (^U^N^P) Pattern not found'
A: Most of the time this is due to a compilation error. To identify the problem do :call g:ClangUpdateQuickFix() followed by :copen. If you do not see any compilation problem and you still have '... Pattern not found' check that the clang library's is correctly loaded (put some debug messages in libclang.py to understand what happens).

##### Clone this wiki locally
You can’t perform that action at this time.
Press h to open a hovercard with more details.