如何向syntastic vim插件添加c++11支持?

时间:2022-05-05 17:26:33

I am using syntastic in my c++11 project. When I am editing in vim, and save (:w) the syntastic plugin gives me errors on every initializer list {} and for each loops which are clearly c++11 features that it's missing.

我在c++11项目中使用了syntastic。当我在vim中编辑并保存(:w)时,syntastic插件会在每个初始化器列表{}和每个循环中出现错误,这些循环显然是c++ +11所缺少的特性。

I installed syntastic using pathogen.

我用病原体进行了合成。

Here are two examples of the error I am getting on initializer lists and for each loops (both c++11 that compile fine):

这里有两个例子说明我在初始化列表和每个循环中遇到的错误(c++11都可以很好地编译):

如何向syntastic vim插件添加c++11支持?如何向syntastic vim插件添加c++11支持?

5 个解决方案

#1


85  

Turns out the C++ linter (syntax checker) of syntastic has many options that can be set on your .vimrc (unfortunate, I wish it was project specific, like the .clang_complete solution).

事实证明,syntastic的c++ linter(语法检查器)有很多选项可以设置在.vimrc上(不幸的是,我希望它是特定于项目的,比如.clang_complete解决方案)。

To enable c++11 standards and use the libc++ library with clang (which is what my project is using) I added the following lines to my ~/.vimrc

为了启用c++11标准,并使用clang(我的项目正在使用的)使用libc++库,我在~/.vimrc中添加了以下几行。

let g:syntastic_cpp_compiler = 'clang++'
let g:syntastic_cpp_compiler_options = ' -std=c++11 -stdlib=libc++'

it now works beautifully.

现在漂亮的工作。

#2


4  

I was facing the same problem and I insist to process c++98 and c++11 separately. below is my solution:

我也遇到了同样的问题,我坚持要分别处理c++98和c++11。下面是我的解决方案:

create file named gcc.vim under bundle/syntastic/syntax_checkers/cpp11/ and copy these to it:

创建名为gcc的文件。vim在bundle/ syntax_checkers/cpp11/下,并将其复制到:

"============================================================================
"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:

that will make gcc checker available (want other checker? you can do the similar things i did for yourself) for files with &filetype == 'cpp11' in vim. how to make your files automatically recongnized as cpp11 filetype in vim? just create file named ext_detect.vim under ~/.vim/ftdetect/ with the following content:

这将使gcc检查器可用(想要其他检查器吗?您可以为vim中的&filetype = 'cpp11'文件执行类似的操作。如何使您的文件在vim中自动识别为cpp11文件类型?只需创建名为ext_探测的文件。在~ / vim。vim/ftdetect/包含以下内容:

au bufnewfile,bufread *.cpp11 set ft=cpp11
au bufnewfile,bufread *.cppx set ft=cpp11

by this way, you can process your *.cpp files as c++98 standard and *.cpp11 or *.cppx as c++11 standard separately, not only syntax checking, but also syntax highlighting (if you need cpp11 syntax highlighting support, this vim plugin will be useful, although not perfect).

通过这种方式,您可以处理您的*。cpp文件为c++98标准和*。cpp11或*。cppx作为c++11的独立标准,不仅要进行语法检查,还要进行语法突出显示(如果需要cpp11语法突出显示支持,这个vim插件将非常有用,尽管不是很完美)。

#3


4  

It has project specific options, like the .clang_complete solution

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

You can set path to files g:syntastic_cpp_config_file and g:syntastic_c_config_file. The default is .syntastic_cpp_config for C++. Put file in root of the project and compiler options inside it (one for each line)

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

for details

有关详细信息,

#4


0  

If your using YouCompleteMe in addition to Syntastic you need to change your .ycm_extra_conf.py file. Sepcifically change '-Wc++98-compat' to '-Wnoc++98-compat'.

如果您使用的是Syntastic,那么您需要更改.ycm_extra_conf。py文件。将“-Wc+ 98-compat”改为“-Wnoc+ 98-compat”。

I didn't have to change the Syntastic settings myself, although that might be because I'm using a compile_commands.json file.

我不必自己更改语法设置,尽管这可能是因为我使用的是compile_commands。json文件。

via here.

通过这里。

#5


0  

If you use YouCompleteMe,maybe you should change '.ycm_extra_conf.py'.only change flags:(file path~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py) ;

如果您使用YouCompleteMe,也许您应该更改'.ycm_extra_conf.py'。只更改标志:(文件路径~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/. ycm_extra_con_con .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/',
 ]

#1


85  

Turns out the C++ linter (syntax checker) of syntastic has many options that can be set on your .vimrc (unfortunate, I wish it was project specific, like the .clang_complete solution).

事实证明,syntastic的c++ linter(语法检查器)有很多选项可以设置在.vimrc上(不幸的是,我希望它是特定于项目的,比如.clang_complete解决方案)。

To enable c++11 standards and use the libc++ library with clang (which is what my project is using) I added the following lines to my ~/.vimrc

为了启用c++11标准,并使用clang(我的项目正在使用的)使用libc++库,我在~/.vimrc中添加了以下几行。

let g:syntastic_cpp_compiler = 'clang++'
let g:syntastic_cpp_compiler_options = ' -std=c++11 -stdlib=libc++'

it now works beautifully.

现在漂亮的工作。

#2


4  

I was facing the same problem and I insist to process c++98 and c++11 separately. below is my solution:

我也遇到了同样的问题,我坚持要分别处理c++98和c++11。下面是我的解决方案:

create file named gcc.vim under bundle/syntastic/syntax_checkers/cpp11/ and copy these to it:

创建名为gcc的文件。vim在bundle/ syntax_checkers/cpp11/下,并将其复制到:

"============================================================================
"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:

that will make gcc checker available (want other checker? you can do the similar things i did for yourself) for files with &filetype == 'cpp11' in vim. how to make your files automatically recongnized as cpp11 filetype in vim? just create file named ext_detect.vim under ~/.vim/ftdetect/ with the following content:

这将使gcc检查器可用(想要其他检查器吗?您可以为vim中的&filetype = 'cpp11'文件执行类似的操作。如何使您的文件在vim中自动识别为cpp11文件类型?只需创建名为ext_探测的文件。在~ / vim。vim/ftdetect/包含以下内容:

au bufnewfile,bufread *.cpp11 set ft=cpp11
au bufnewfile,bufread *.cppx set ft=cpp11

by this way, you can process your *.cpp files as c++98 standard and *.cpp11 or *.cppx as c++11 standard separately, not only syntax checking, but also syntax highlighting (if you need cpp11 syntax highlighting support, this vim plugin will be useful, although not perfect).

通过这种方式,您可以处理您的*。cpp文件为c++98标准和*。cpp11或*。cppx作为c++11的独立标准,不仅要进行语法检查,还要进行语法突出显示(如果需要cpp11语法突出显示支持,这个vim插件将非常有用,尽管不是很完美)。

#3


4  

It has project specific options, like the .clang_complete solution

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

You can set path to files g:syntastic_cpp_config_file and g:syntastic_c_config_file. The default is .syntastic_cpp_config for C++. Put file in root of the project and compiler options inside it (one for each line)

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

for details

有关详细信息,

#4


0  

If your using YouCompleteMe in addition to Syntastic you need to change your .ycm_extra_conf.py file. Sepcifically change '-Wc++98-compat' to '-Wnoc++98-compat'.

如果您使用的是Syntastic,那么您需要更改.ycm_extra_conf。py文件。将“-Wc+ 98-compat”改为“-Wnoc+ 98-compat”。

I didn't have to change the Syntastic settings myself, although that might be because I'm using a compile_commands.json file.

我不必自己更改语法设置,尽管这可能是因为我使用的是compile_commands。json文件。

via here.

通过这里。

#5


0  

If you use YouCompleteMe,maybe you should change '.ycm_extra_conf.py'.only change flags:(file path~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py) ;

如果您使用YouCompleteMe,也许您应该更改'.ycm_extra_conf.py'。只更改标志:(文件路径~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/. ycm_extra_con_con .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/',
 ]