在Ubuntu 16.10上,Clang工具找不到所需的库

3

我正在使用CMake在Ubuntu 16.10 x64上构建一个名为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.

我找到了这个bug,但安装clang-3.9似乎并没有帮助我的情况。

如果有建议,请告诉我。谢谢。


请查看我几年前在askubuntu上提出的问题。在Ubuntu上,Clang++似乎经常出现系统故障。http://askubuntu.com/questions/383029/clang-version-3-3-lacks-headers - Dirk Eddelbuettel
你不能期望clang C++库与g++兼容。 - stark
似乎在Ubuntu上使用Clang存在许多困难,但我还没有找到答案。我想在这里发布一下。 - Bryan Muscedere
1个回答

5

当你同时安装了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中。

希望对您有所帮助。


如果有人想要透明地来回切换怎么办?我有不止一个工作编辑器、终端等,那为什么不能有两个编译器呢? - Dirk Eddelbuettel
我所做的是在终端上导出INCLUDE_PATH,我计划进行与clang相关的工作。我不会声称自己是如何工作的设计决策方面的专家。但是我建议我的朋友编写一个bash函数来在包含路径之间切换,他似乎很满意。 - Nishant Sharma
谢谢你的帖子,Nishant。 我一回家就要试试这个! - Bryan Muscedere

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接