Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

path setting for c++ include headers for vim

My vim has path settings as shown below.

path=.,/usr/include,,

I think this is a default setting of 'path' I guess.

Because of this, g f opens c header files under the cursor.

But on C++ file C++ header files are not opened because the C++ header file location is not added to path variable of vim.

set path+=/usr/include/c++/4.6

I think that this setting on vimrc would be a solution. But the problem is the actual directory location for C++ header file would be changed in every different linux distributions and g++ compiler versions.

How can I set path for c++ header files in a portable manner?

let g:gcpp_headers_path = system("g++ --version | grep g++ | awk '{print \"/usr/include/c++/\"$NF}'")

execute 'set path+=' . g:gcpp_headers_path

Now I am using this above: This works with g++ environment. Not tested with other compilers.

like image 649
Jumogehn Avatar asked Apr 10 '13 00:04

Jumogehn


2 Answers

If there's a limited number of locations, a simple conditional in ~/.vimrc will do:

if isdirectory('/usr/include/c++/4.6')
    set path+=/usr/include/c++/4.6
elseif isdirectory(...

If you have a lot of different systems, and don't want to maintain all variations in a central place, you can move the system-dependent settings to a separate, local-only file, and invoke that from your ~/.vimrc, like this:

" Source system-specific .vimrc first.
if filereadable(expand('~/local/.vimrc'))
    source ~/local/.vimrc
endif
like image 74
Ingo Karkat Avatar answered Oct 25 '22 17:10

Ingo Karkat


I recently had the same problem, so here is my solution for documentation purposes:

1) I added the following to my .bashrc:

# add additional search paths to vim.
VIM_EXTPATHS="$HOME/.vim.extpaths"
if [ ! -e "$VIM_EXTPATHS" ] || [ "/usr/bin/cpp" -nt "$VIM_EXTPATHS" ]; then
    echo | cpp -v 2>&1 | \
    awk '/^#include </ { state=1 } /End of search list/ { state=0 } /^ / && state { print "set path+=" substr($0, 2) "/**2" }' > $VIM_EXTPATHS
fi

2) I added the following to my .vimrc:

" add extra paths.
let s:extpaths=expand("$HOME/.vim.extpaths")
if filereadable(s:extpaths)
    execute "source ".s:extpaths
endif

On my system, the contents of the .vim.extpaths file are as follows:

set path+=/usr/lib/gcc/x86_64-linux-gnu/8/include/**2
set path+=/usr/local/include/**2
set path+=/usr/lib/gcc/x86_64-linux-gnu/8/include-fixed/**2
set path+=/usr/include/x86_64-linux-gnu/**2
set path+=/usr/include/**2

The **2 means that ViM will search two directories deep inside these directories. Now gf will find all the C++ headers I need. If you increase the depth, searches will take a lot more time, so don't set this number too high.

@note: for #include <chrono>, ViM will go to /usr/include/boost/chrono, which, funny enough, is a directory. I wonder why go file will open a directory, maybe this should be reported as a bug. To get to the correct chrono header you have to type 2gf.

like image 35
hochl Avatar answered Oct 25 '22 17:10

hochl