12

I have the following file foo.cpp:

#include <vector>

struct MyClass
{
  std::vector<int> v;
};

It can be successfully compiled with clang (I'm using clang 3.3 on Ubuntu 13.04 32bit):

clang++ -c foo.cpp

Now I want to print AST:

clang++ -cc1 -ast-print foo.cpp

and I've got the following error

foo.cpp:1:10: fatal error: 'vector' file not found
#include <vector>
         ^
struct MyClass {
};
1 error generated.

It looks like clang++ -cc1 doesn't know about system include files etc. I'm wondering how to set up includes for clang++ -cc1?

Rom098
  • 2,445
  • 4
  • 35
  • 52

3 Answers3

9

You need to set up the right include paths. on my system I added

-I/usr/include/i386-linux-gnu/c++/4.8 -I/usr/include/c++/4.8 

to the compiler flags. The first one was so that it could find bits/c++config.h Of course the 4.8 is due to the fact I am using a compiler compatible with g++-4.8

I also added

-std=c++11 -stdlib=libstdc++

as compiler options. Hope this helps

user1741137
  • 4,949
  • 2
  • 19
  • 28
  • Also try changing "-stdlib=libstdc++" to "-lstdc++", i.e. "clang -x c++ -std=c++11 -lstdc++ -Wall input.cpp -o output" – robor Aug 03 '16 at 15:31
8

It's a Frequently Asked Question

john
  • 85,011
  • 4
  • 57
  • 81
4

@john is correct. For posterity, the relevant portions of the FAQ are (with names tweaked to match the question) :

clang -cc1 is the frontend, clang is the driver. The driver invokes the frontend with options appropriate for your system. To see these options, run:

$ clang++ -### -c foo.cpp

Some clang command line options are driver-only options, some are frontend-only options. Frontend-only options are intended to be used only by clang developers. Users should not run clang -cc1 directly, because -cc1 options are not guaranteed to be stable.

If you want to use a frontend-only option (“a -cc1 option”), for example -ast-dump, then you need to take the clang -cc1 line generated by the driver and add the option you need. Alternatively, you can run clang -Xclang <option> ... to force the driver [to] pass <option> to clang -cc1.

I did the latter (-Xclang) for emitting precompiled headers:

/usr/bin/clang++ -x c++-header foo.hpp -Xclang -emit-pch -o foo.hpp.pch <other options>
                                       ^^^^^^^

Without the -Xclang, clang++ ignored the -emit-pch. When I tried -cc1, I had the same problem as the OP — clang++ accepted -emit-pch but didn't have the other options the driver normally provides.

Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81