Clang 工具无法在 Ubuntu 16.10 上找到所需的库

Clang Tool Cannot Find Required Libraries on Ubuntu 16.10

我正在 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 似乎对我的情况没有帮助。

如有任何建议,我们将不胜感激。

当您同时安装了 gcc 和 clang 时,有时会出现此问题。默认设置 C_INCLUDE_PATHCPLUS_INCLUDE_PATH 搜索 gcc 自己的包含文件,不包含 clang 的包含文件。 Clang 需要特定于 clang 的包含文件。要修复尝试:

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

希望对您有所帮助。