llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.38k stars 12.15k forks source link

Standard Headers missing from Clang tool #93957

Open spino17 opened 6 months ago

spino17 commented 6 months ago

I am building a clang tool and it works fine if I don't include any headers but when I am including <iostream> or <string> it throws: 'iostream' file not found. How do I include the standard lib headers to the tool in a "os agnostic way" (without specifying the exact path to the headers on system) ?

DimitryAndric commented 6 months ago

Normally, clang should automatically find your system headers. Typically it helps if you install libstdc++-devel or similar packages, or ensure in some other way that C++ headers are available on your system. Please provide more information about what you are doing exactly, which operating system and distro you are using, etc.

spino17 commented 6 months ago

Thanks @DimitryAndric for the comment. So I am trying the libtooling tutorial from: https://clang.llvm.org/docs/LibASTMatchersTutorial.html and have the following simple code.

// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"

using namespace clang::tooling;
using namespace llvm;

// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static llvm::cl::OptionCategory MyToolCategory("my-tool options");

// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);

// A help message for this specific tool can be added afterwards.
static cl::extrahelp MoreHelp("\nMore help text...\n");

int main(int argc, const char **argv) {
  auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
  if (!ExpectedParser) {
    // Fail gracefully for unsupported options.
    llvm::errs() << ExpectedParser.takeError();
    return 1;
  }
  CommonOptionsParser& OptionsParser = ExpectedParser.get();
  ClangTool Tool(OptionsParser.getCompilations(),
                 OptionsParser.getSourcePathList());
  return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
}

so when I am writing the command bin/loop-convert test.cpp -- it works for the below program for test.cpp

int main() {
    return 0;
}

However when I am including the standard headers like in test.cpp:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, World" << endl;
}

I am getting the following error: fatal error: 'iostream' file not found.

SmartAI commented 3 months ago

Add extra argument to your program: --extra-args=-I/path/to/clang/include

the path is usually: $INSTALL_PREFIX/lib/clang/$VERSION/include