Using clang or g++/gcc to print preprocessed code without including files from system paths

Viewed 847

currently I'd like to debug my includes. To do this I want to get the preprocessed code, but without any system header files - just my own code.

I tried it with flag "-E" and "-nostdinc". But then I get errors that for example <string> is missing. How to disable such preprocessing errors?

3 Answers

How to disable such preprocessing errors?

You could create a set of headers files with names matching the standard library headers. Put those headers in the include directory. Let the files be empty.

Using clang or g++/gcc to print preprocessed code without including files from system paths

I can see two other approaches besides the empty header approach:

  • Instead of using the full pre-processor of the compiler, write your own pre-processor that only does the subset of processing that you want.

  • Or, write a post-pre-processor that removes the standard header content from the pre-processed result.

Using clang or g++/gcc to print preprocessed code without including files from system paths

This is not easily possible with GCC. Read about how to invoke GCC.

But you could get all the preprocessed code using g++ -C -E and use some script (perhaps with GNU gawk) to remove the useless parts.

currently I'd like to debug my includes.

I have the habit of generating all the preprocessed code and then use GNU less to look inside it. Disk space is cheap.

Alternatively, consider writing your own GCC plugin doing what you need.

(It's not really an answer - just a "hack")

To solve this I created a text file with all system headers by:

rem my GCC STL-PATH
cd Z:\usr\include\c++\10

dir /b > F:\DummySTL\files.txt

Then I executed the following line of code:

for /f "delims=" %F in (files.txt) do copy nul "%F"

This creates an empty text file for every line in the file. Now I can call gcc or clang just with:

-isystem"F:\DummySTL"
Related