xavierd / clang_complete

Vim plugin that use clang for completing C/C++ code.
http://www.vim.org/scripts/script.php?script_id=3302
1.95k stars 308 forks source link

clang_complete is ignoring compiler flags in compile_commands.json #564

Open gunslingerfry opened 6 years ago

gunslingerfry commented 6 years ago

An example of my compile_commands.json file generated by using cmake/clang

[
{
  directory: build-dir,
  command: /usr/bin/clang++  -Dnz_shared_EXPORTS -I/home/matthew/nz/include -isystem /home/matthew/nz/include/zip  -g -fPIC   -Wall -Werror -DOS_LINUX -O0 -I/usr/lib/clang/3.8/include -std=gnu++14 -o CMakeFiles/nz_shared.dir/src/boost_error_code.cpp.o -c /home/matthew/nz/src/boost_error_code.cpp,
  file: /home/matthew/nz/src/boost_error_code.cpp
},
... etc.
]

clang_complete ignores the -std=gnu++14 and the -DOS_LINUX and generates errors because of them. Setting g:clang_user_options='-std=gnu++14 -DOS_LINUX' in my .vimrc fixes the issue.

xaizek commented 6 years ago

Does your g:clang_auto_user_options include compile_commands.json as per documentation?

gunslingerfry commented 6 years ago

It was not. let g:clang_auto_user_options='compile_commands.json, path' however, makes no difference.

gunslingerfry commented 6 years ago

The rest of my configuration:

let g:clang_library_path='/usr/lib/llvm-3.8/lib/libclang.so.1'
let g:clang_complete_auto=1
let g:clang_user_options='-std=gnu++14 -DOS_LINUX'
let g:clang_hl_errors=1
let g:clang_use_library=1
let g:clang_compilation_database=GetCompileDatabase()
let g:clang_complete_copen=1
let g:clang_close_preview=1
set completeopt=longest,menuone
function! GetCompileDatabase()
    let l:curPathVar = '%:p'
    let l:curPath = expand(l:curPathVar)
    let l:allPaths = l:curPath
    while l:curPath !=# '/'
        let l:allPaths .= ',' . l:curPath
        let l:curPathVar .= ':h'
        let l:curPath = expand(l:curPathVar)
    endwhile
    return fnamemodify(globpath(l:allPaths, 'compile_commands.json'), ':h')
endfunction

Function just gets the path of the compile_commands.json file. It's at the root of the project but I was getting errors when I opened source files in subdirectories.

xaizek commented 6 years ago

I've never tried using compilation database, maybe see #165, there was some discussion about absolute vs. relative paths.

gunslingerfry commented 5 years ago

FWIW, I don't know if the compile database support is in the clang_complete plugin or the clang library, but I gave up on this. I just switched to the .clang_complete file. It's simple enough as long as you are writing the code and giving paths relative to the main include directory i.e. #include "mylib/myheader.h" then my .clang_complete file becomes

-Iinclude
-DOS_LINUX
--std=c++14

which is plenty simple.