Bryan Muscedere
Bryan Muscedere

Reputation: 571

Clang Tool Cannot Find Required Libraries on Ubuntu 16.10

I am building a simple C++ Clang tool program called ClangEx using CMake on Ubuntu 16.10 x64.

The project has a single main.cpp file. It's contents are as follows:

#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());
}

It builds successfully with CMake but when I use it to analyze an example C++ program, I get the following error:

$ ./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.

I was able to find this bug but installing clang-3.9 doesn't seem to help my situation.

Any advice would be appreciated.

Upvotes: 3

Views: 2322

Answers (1)

Nishant Sharma
Nishant Sharma

Reputation: 693

This problem arises sometimes when you have both gcc and clang installed. By default C_INCLUDE_PATH and CPLUS_INCLUDE_PATH are set to search gcc's own include files and don't include clang's include files. And Clang needs the clang specefic include files. To fix try:

export C_INCLUDE_PATH=$C_INCLUDE_PATH:"<clang_include_path>"    
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:"<clang_include_path>"

where <clang_include_path> is usually /usr/lib/clang/<version>/include, but can vary based on your installation. On my system, since I built clang from source it's totally different.

If you permanently want to export the two flags, add the same two lines to ~/.bashrc

Hope that helps.

Upvotes: 5

Related Questions