我在我的 c++11 项目中使用 syntastic。当我在 vim 中编辑并保存 (:w) 时,syntastic 插件在每个初始化列表 {} 和每个循环上都会给我错误,这显然是它缺少的 c++11 功能。
我使用病原体安装了 syntastic。
这是我在初始化程序列表和每个循环中遇到的错误的两个示例(两个 c++11 都可以正常编译):
结果是 syntastic 的 C++ linter(语法检查器)有很多选项可以在你的 .vimrc 上设置(不幸的是,我希望它是特定于项目的,比如 .clang_complete 解决方案)。
为了启用 c++11 标准并使用带有 clang 的 libc++ 库(这是我的项目正在使用的),我将以下行添加到我的 ~/.vimrc
let g:syntastic_cpp_compiler = 'clang++'
let g:syntastic_cpp_compiler_options = ' -std=c++11 -stdlib=libc++'
它现在工作得很好。
我遇到了同样的问题,我坚持要分别处理 c++98 和 c++11。以下是我的解决方案:
在bundle/syntastic/syntax_checkers/cpp11/下创建名为gcc.vim的文件并将其复制到其中:
"============================================================================
"File: cpp11.vim
"Description: Syntax checking plugin for syntastic.vim
"Maintainer: Gregor Uhlenheuer <kongo2002 at gmail dot com>
"License: This program is free software. It comes without any warranty,
" to the extent permitted by applicable law. You can redistribute
" it and/or modify it under the terms of the Do What The Fuck You
" Want To Public License, Version 2, as published by Sam Hocevar.
" See http://sam.zoy.org/wtfpl/COPYING for more details.
"
"============================================================================
if exists('g:loaded_syntastic_cpp11_gcc_checker')
finish
endif
let g:loaded_syntastic_cpp11_gcc_checker = 1
if !exists('g:syntastic_cpp11_compiler')
let g:syntastic_cpp11_compiler = executable('g++') ? 'g++' : 'clang++'
endif
if !exists('g:syntastic_cpp11_compiler_options')
let g:syntastic_cpp11_compiler_options = '-std=c++11'
endif
let s:save_cpo = &cpo
set cpo&vim
function! SyntaxCheckers_cpp11_gcc_IsAvailable() dict
return executable(expand(g:syntastic_cpp11_compiler))
endfunction
function! SyntaxCheckers_cpp11_gcc_GetLocList() dict
return syntastic#c#GetLocList('cpp11', 'gcc', {
\ 'errorformat':
\ '%-G%f:%s:,' .
\ '%f:%l:%c: %trror: %m,' .
\ '%f:%l:%c: %tarning: %m,' .
\ '%f:%l:%c: %m,'.
\ '%f:%l: %trror: %m,'.
\ '%f:%l: %tarning: %m,'.
\ '%f:%l: %m',
\ 'main_flags': '-x c++ -fsyntax-only',
\ 'header_flags': '-x c++',
\ 'header_names': '\m\.\(h\|hpp\|hh\)$' })
endfunction
call g:SyntasticRegistry.CreateAndRegisterChecker({
\ 'filetype': 'cpp11',
\ 'name': 'gcc' })
let &cpo = s:save_cpo
unlet s:save_cpo
" vim: set et sts=4 sw=4:
这将使 gcc 检查器可用(想要其他检查器?你可以为自己做类似的事情)对于 vim 中具有 &filetype == 'cpp11' 的文件。如何使您的文件在vim中自动识别为cpp11文件类型?只需在~/.vim/ftdetect/下创建名为ext_detect.vim的文件,其内容如下:
au bufnewfile,bufread *.cpp11 set ft=cpp11
au bufnewfile,bufread *.cppx set ft=cpp11
通过这种方式,您可以将 *.cpp 文件作为 c++98 标准和 *.cpp11 或 *.cppx 作为 c++11 标准分别处理,不仅可以进行语法检查,还可以进行语法高亮(如果您需要 cpp11 语法高亮支持,这个 vim 插件会很有用,虽然不完美)。
它具有特定于项目的选项,例如 .clang_complete 解决方案
您可以设置文件 g:syntastic_cpp_config_file 和 g:syntastic_c_config_file 的路径。对于 C++,默认值为 .syntastic_cpp_config。将文件放在项目的根目录中,并在其中放置编译器选项(每行一个)
详情_
如果除了 Syntastic 之外还使用 YouCompleteMe,您需要更改 .ycm_extra_conf.py 文件。将“-Wc++98-compat”单独更改为“-Wnoc++98-compat”。
我不必自己更改 Syntastic 设置,尽管这可能是因为我使用的是 compile_commands.json 文件。
通过这里。
如果您使用 YouCompleteMe,也许您应该更改 '.ycm_extra_conf.py'。仅更改标志:(file path~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py) ;
flags = [
'-std=c++11',
'-O0',
'-Werror',
'-Weverything',
'-Wno-documentation',
'-Wno-deprecated-declarations',
'-Wno-disabled-macro-expansion',
'-Wno-float-equal',
'-Wno-c++98-compat',
'-Wno-c++98-compat-pedantic',
'-Wno-global-constructors',
'-Wno-exit-time-destructors',
'-Wno-missing-prototypes',
'-Wno-padded',
'-Wno-old-style-cast',
'-Wno-weak-vtables',
'-x',
'c++',
'-I',
'.',
'-isystem',
'/usr/include/',
]