如何将C++11支持添加到Syntastic Vim插件?

54

我在使用c++11编写的项目中使用了syntastic插件。 当我在vim中编辑并保存(:w)时,syntastic插件会报错,指出每个初始化列表{}和for each循环都是c++11功能,但它缺少这些功能。

我使用pathogen安装了syntastic。

以下是我在初始化列表和for each循环上遇到的两个错误示例(它们都是c++11且可以正常编译):

初始化列表上的错误 for each循环上的错误


1
:echo b:clang_user_options 命令在属于该项目的缓冲区中的输出是什么?如果你的假设是正确的,那么就不会有“-std=c++”子字符串。 - xaizek
:echo b:clang_user_options 的输出是 -I/usr/include -std=c++11 -stdlib=libc++。所以我不明白为什么会出现与初始化列表和 for each 循环相关的错误。我将在主贴中添加一个示例错误。 - Carneiro
原来这不是clang_complete,而是syntastic。我会更新帖子和答案。 - Carneiro
5个回答

101

事实证明,syntastic的C++语法检查器有许多选项可以在您的.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++'

现在它的运作非常完美。


我检查了clang++和g++选项,它们是一样的,所以我用"g++"替换了"clang++",但还是不起作用。 - rafee
我认为你不能使用-stdlib=libc++来运行g++。 - Carneiro
我也尝试过不加-stdlib=libc++,但那也没有成功。 - rafee
实际上我是在没有使用-stdlib的情况下编译的,但是当正常设置不成功时,我将-stdlib放入了.vimrc文件中。 - rafee
请确保您已经安装了Clang编译器!!对我来说,所有的错误都没有一起出现,后来我意识到我需要安装Clang编译器 =P - Jacob Minshall
2
对我来说,我必须删除“-stdlib=libc++”才能使其正常工作。 - Deqing

4
我是一个可以翻译文本的有用助手。

我曾遇到同样的问题,坚持要单独处理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 checker 在 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标准和c++11标准,不仅进行语法检查,还可进行语法高亮显示(如果需要cpp11语法高亮支持,则此vim插件会很有帮助,尽管不是完美的)。

4

它具有项目特定选项,如.clang_complete解决方案

您可以设置文件的路径g:syntastic_cpp_config_file和g:syntastic_c_config_file。默认值为.syntastic_cpp_config用于C ++。将文件放在项目根目录中,并在其中添加编译器选项(每行一个)

详情请参见此处


0
如果您使用YouCompleteMe,可能需要更改'.ycm_extra_conf.py'文件。只需更改标志:(文件路径〜/ .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/',
 ]

0
如果您同时使用Syntastic和YouCompleteMe,需要更改.ycm_extra_conf.py文件。具体来说,将“-Wc++98-compat”更改为“-Wnoc++98-compat”。
我自己没有更改Syntastic设置的必要,尽管这可能是因为我正在使用compile_commands.json文件。 通过此处

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接