我在使用c++11编写的项目中使用了syntastic插件。 当我在vim中编辑并保存(:w)时,syntastic插件会报错,指出每个初始化列表{}和for each循环都是c++11功能,但它缺少这些功能。
我使用pathogen安装了syntastic。
以下是我在初始化列表和for each循环上遇到的两个错误示例(它们都是c++11且可以正常编译):
事实证明,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++'
现在它的运作非常完美。
我曾遇到同样的问题,坚持要单独处理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
它具有项目特定选项,如.clang_complete解决方案
您可以设置文件的路径g:syntastic_cpp_config_file和g:syntastic_c_config_file。默认值为.syntastic_cpp_config用于C ++。将文件放在项目根目录中,并在其中添加编译器选项(每行一个)
详情请参见此处
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/',
]
:echo b:clang_user_options
命令在属于该项目的缓冲区中的输出是什么?如果你的假设是正确的,那么就不会有“-std=c++”子字符串。 - xaizek:echo b:clang_user_options
的输出是-I/usr/include -std=c++11 -stdlib=libc++
。所以我不明白为什么会出现与初始化列表和 for each 循环相关的错误。我将在主贴中添加一个示例错误。 - Carneiro