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

标签 c++ ubuntu clang llvm-clang

我正在 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

希望对您有所帮助。

关于c++ - Clang 工具无法在 Ubuntu 16.10 上找到所需的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40431161/

相关文章:

c++ - 引用 this 指针 : GCC vs clang

c++ - 如何声明一个未知大小的数组,然后获取输入直到我需要,然后获取数组的大小?

c++ - HANDLE 指向的结构

c++ - 定义一个在全局范围内运行的宏,没有警告

mysql - 在ubuntu 18.04上安装mariadb 10.3 gpg添加 key 错误gpg : keyserver receive failed: Server indicated a failure

java - JNA - Ubuntu 上的 java.lang.UnsatisfiedLinkError

c++ - GCC 和 Clang 模板调用解析差异

c++ - 包含引用的 STL 映射无法编译

ubuntu - cargo +夜间错误: no such subcommand

c++ - 如何将 libc++ 与调试符号链接(symbolic link)?