Clang -cc1 and the system includes

I have the following foo.cpp file:

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

It can be compiled with clang (I use 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 got the following error:

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

It seems that clang++ -cc1 does not know about system files, etc. I am wondering how to configure inclusion for clang++ -cc1 ?

+6
source share
3 answers
+7
source

You need to configure the correct included paths. on my system I added

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

for compiler flags. The first one was such that he could find the / C ++ config.h bit. Of course, 4.8 is due to the fact that I use a compiler compatible with g ++ - 4.8

I also added

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

as compiler options. Hope this helps

+7
source

@john is correct . For posterity, the relevant parts of the FAQ (with the names configured to answer the question):

clang -cc1 is an interface, clang is a driver. The driver invokes an interface with parameters suitable for your system. To view these options, run:

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

Some clang command-line options are driver-only options, some of them are interface-only options. Frontend-only options are for use by clang developers only. Users should not run clang -cc1 directly because the -cc1 options -cc1 not guaranteed.

If you want to use the option only for the interface ("a -cc1 option"), for example -ast-dump , you need to take the clang -cc1 line generated by the driver and add the required parameter. Alternatively, you can run clang -Xclang <option> ... to force the driver [switch] <option> to clang -cc1 .

I did the last ( -Xclang ) to emit precompiled headers:

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

Without -Xclang , clang++ ignores -emit-pch . When I tried -cc1 , I had the same problem as the OP - clang++ accepted -emit-pch , but does not have other parameters that the driver usually provides.

0
source

Source: https://habr.com/ru/post/952723/


All Articles