C++11 为c+配置叮当检查+;标准库

C++11 为c+配置叮当检查+;标准库,c++11,clang++,C++11,Clang++,我试图将Ale作为我的linter运行,它反过来使用叮当声检查来lint我的代码 $ clang-check FeatureManager.h Error while trying to load a compilation database: Could not auto-detect compilation database for file "FeatureManager.h" No compilation database found in /home/babbleshack/ or a

我试图将Ale作为我的linter运行,它反过来使用叮当声检查来lint我的代码

$ clang-check FeatureManager.h
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "FeatureManager.h"
No compilation database found in /home/babbleshack/ or any parent directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
/home/babbleshack/FeatureManager.h:6:10: fatal error: 'unordered_map' file not found
#include <unordered_map>
         ^~~~~~~~~~~~~~~
1 error generated.
Error while processing /home/babbleshack/FeatureManager.h.

没有可用于clang check的标志,允许我设置编译标志。

花了一些时间来解决这个问题,但您可以这样做

clangcheck file.cxx--Wall-std=c++11-xc++

或者如果你使用的是叮当声

clangtidy file.cxx--Wall-std=c++11-xc++

为了让两者都能使用ALE,我在vimrc中添加了以下内容

让g:ale_cpp_clangtidy_options='-Wall-std=c++11-xc++'
设g:ale_cpp_clangcheck_options='--Wall-std=c++11-xc++'


如果你想让ALE也为C工作,你就必须为g:ALE_C_clangtidy_options和g:ALE_C_clangcheck_options做同样的事情,我被类似的错误消息难住太久了:

/my/project/src/util.h:4:10: error: 'string' file not found [clang-diagnostic-error]
#include <string>
         ^

正如克里斯所指出的,关键区别是<代码> -x c标题FLAG,这是因为这意味着系统C++不被用于处理<代码> UTI.H./COD> < /P> 但是,

-主文件名
标志对我来说也很奇怪;为什么头文件会成为主文件?在深入研究的过程中,我还发现头文件首先不应该直接编译!使用
src/*.cc
而不是
src/*.{h,cc}
完全避免了这个问题,因为从一开始就不要求Clang尝试自己处理
.h

不过,这确实带来了另一个问题。默认情况下不会报告这些头文件中的错误,因为它们不是您要求查看的文件。这就是“Use-header filter=.to display errors from all non-system header.*”消息
clangtidy
prints出现的地方。如果我通过
-header filter=src/*
(只包括我的
src
头文件,而不包括我在
-I
中包含的任何其他头文件),我会在头文件中看到预期的错误。呸


我不确定通常是喜欢
-xC++
还是
-header filter=.*
-header-filter
的一个缺点是您必须调整filter正则表达式,而不仅仅是传递要检查的文件。但是另一方面,孤立地处理头文件是一项浪费性的工作(我希望C++能够在一个更大的项目中快速地增加)。例如,我有一个包含
的头文件,但是
无效参数的叮当检查错误'-std=c++11'不允许与'c'
一起使用?哦!抓得好@Babbleshack!我通过添加
-xc++
标志来实现这一点。我将更新我的答案以反映这一点。谢谢,你在哪里找到了用于clang check/tidy的手册页?顺便说一句,我没有任何用于clang check/tidy的手册页,但是你可以使用
clang check--help
clang tidy--help
获取一些帮助页。但是,他们没有提到使用
--
(这里有一个非常简短的介绍)。基本上,我(有限的)理解是,
--
标志之后的选项作为选项传递给clang编译器,您可以通过
man clang
看到这些选项。如果文档更好的话会有帮助的…非常感谢!我浪费了一天的时间试图让线绳起作用,而你的回答最终成功了。
/my/project/src/util.h:4:10: error: 'string' file not found [clang-diagnostic-error]
#include <string>
         ^
$ clang-tidy ... src/*.{h,cc} -- ... -v
...
clang-tool ... -main-file-name util.cc ... -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9 ... -x c++ ... /tmp/copy/src/util_test.cc
...
clang-tool ... -main-file-name util.h ... -x c-header /my/project/src/util.h
...