我正在使用Ubuntu 16.10 x64上的CMake构建一个名为ClangEx
的简单C ++ Clang工具程序。
该项目只有一个main.cpp
文件。它的内容如下:
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
using namespace clang::tooling;
using namespace llvm;
static llvm::cl::OptionCategory MyToolCategory("my-tool options");
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...");
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
}
使用CMake成功构建但是当我用它来分析示例C ++程序时,我收到以下错误:
$ ./ClangEx SandwichBar.cpp --
In file included from /home/bmuscede/SandwichBar.cpp:11:
In file included from /home/bmuscede/SandwichBar.h:14:
In file included from /home/bmuscede/Customers/Sandwich.h:15:
In file included from /home/bmuscede/Customers/../Capital/Recipe.h:14:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/string:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/bits/char_traits.h:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/bits/postypes.h:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/cwchar:44:
/usr/include/wchar.h:39:11: fatal error: 'stdarg.h' file not found
# include <stdarg.h>
^
1 error generated.
Error while processing /home/bmuscede/SandwichBar.cpp.
我能够找到this bug,但安装clang-3.9
似乎无助于我的情况。
任何建议都将受到赞赏。
答案 0 :(得分:3)
如果同时安装了gcc和clang,则会出现此问题。默认情况下,C_INCLUDE_PATH
和CPLUS_INCLUDE_PATH
设置为搜索gcc自己的包含文件,而不包含clang的包含文件。而且Clang需要clang specefic包含文件。要修复尝试:
export C_INCLUDE_PATH=$C_INCLUDE_PATH:"<clang_include_path>"
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:"<clang_include_path>"
其中<clang_include_path>
通常为/usr/lib/clang/<version>/include
,但可能会因您的安装而异。在我的系统上,因为我从源代码构建了clang,它完全不同。
如果您想永久导出两个标记,请将相同的两行添加到~/.bashrc
希望有所帮助。