Clang Complete

From the Linux and Unix Users Group at Virginia Teck Wiki
Jump to: navigation, search

Clang Complete

Clang Complete is an addon to the popular text editor vim. It allows the user to use clang to do a lightweight compile/parse of their code, in order to allow extremely accurate code autocompletion. Alternatives out there (gcc_complete/ctags) either use hackish methods (modified compilers) or do manual parsing of the file. Clang was built with ease of IDE integration built-in, so Clang Complete simply builds on that feature.

Installation

First off, you obviously need to install clang, the C/C++/ObjectiveC compiler. Use your favorite package manager (or compilation tools) to make/install clang.

The installation of Clang Complete requires a version of vim that supports python(3) addons. At the moment, Arch Linux's default vim build lacks the ability to use python extensions, and gvim lacks support for python3. In order to check if your vim install supports these, do:

vim --version | grep -i python

If you get a result and it contains the following, you can skip reinstalling python. Otherwise, you have to reinstall vim. (Note: if you see this with -, as in -python/dyn -python3/dyn, you have to reinstall)

+python/dyn +python3/dyn

Now we get to recompile vim from source. If you have a fun distro-specific tool such as ABS, feel free to use that. Otherwise, pull the sources from http://www.vim.org/sources.php and follow along.

You can compile/install vim as usual. The sole special thing that need be done for this compilation is that, when you call ./configure, you *need* to ensure the following flags are present:

--enable-python3interp=dynamic --enable-pythoninterp=dynamic

and the following are absent

--disable-python3interp --disable-pythoninterp

Otherwise Clang Complete will fail.


Now that we have the necessary version of vim installed, you need to clone the most recent sources of Clang Complete into a new directory using the following command:

git clone https://github.com/Rip-Rip/clang_complete.git

Now to install it, do:

cd clang_complete
make install

And it should now be installed. Before you rush off and see how great it is, I suggest you look at the documentation to see what configuration options you want. Personally, I use the following:

" Clang Complete Settings
let g:clang_use_library=1
" if there's an error, allow us to see it
let g:clang_complete_copen=1
let g:clang_complete_macros=1
let g:clang_complete_patterns=0
" Limit memory use
let g:clang_memory_percent=70
" Remove -std=c++11 if you don't use C++ for everything like I do.
let g:clang_user_options=' -std=c++11 || exit 0'
" Set this to 0 if you don't want autoselect, 1 if you want autohighlight,
" and 2 if you want autoselect. 0 will make you arrow down to select the first
" option, 1 will select the first option for you, but won't insert it unless you
" press enter. 2 will automatically insert what it thinks is right. 1 is the most
" convenient IMO, and it defaults to 0.
let g:clang_auto_select=1

set conceallevel=2
set concealcursor=vin
let g:clang_snippets=1
let g:clang_conceal_snippets=1
" The single one that works with clang_complete
let g:clang_snippets_engine='clang_complete'

Now you're all set and ready to go! Enjoy autocomplete

Other Include Paths

So, you want to use something other than the standard libraries? Maybe Qt? Maybe Gtk? You've probably noticed autocomplete works for none of these without special configuration.

In order to specify other folders for autocomplete to search for headers, you need to place them (one per line, prefixed directly with '-I') into a .clang_complete file in the current working directory. (This file directly appends each line to the clang command, so if you need to use the C99 standard for a project, for example, you can do -std=c99 on a line of its own).

An example of this file using the c++0x standard, and including Qt headers and libs from the current directory, is:

-std=c++0x
-I/usr/include/QtGui/
-I/usr/include/QtCore/
-I.

Troubleshooting

Clang Complete depends on a mostly working environment to work. It will generally not autocomplete from broken header files, etc. So, type the following into vim to see if it's your code or clang complete that is messed up:

#include <string>

int main()
{
    std::string s;
    s.
}

If autocomplete pops up when you press the '.', it's your code/configuration that's not functioning properly. If autocomplete fails to pop up even when you press <C-x><C-u> (autocomplete), then the install probably has an issue. See [Other Include Paths] for more information if Clang Complete isn't working on a certain library.