4

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?

Bernd
  • 2,113
  • 8
  • 22
  • Why do you need to avoid system included headers? – Basile Starynkevitch Jun 25 '20 at 15:05
  • 1
    GCC has a [script](https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=contrib/uninclude;hb=HEAD) to replace standard header contents with `#include <...>`. It's aimed for compiler development, but maybe you can adjust it for your need. – cpplearner Jun 25 '20 at 15:12
  • 1
    Almost duplicate of [c++ - How do I run the preprocessor on local headers only? - Stack Overflow](https://stackoverflow.com/questions/20889460/how-do-i-run-the-preprocessor-on-local-headers-only) (except that in this case you want to delete the include instead of keep it unchanged) – user202729 Sep 22 '20 at 08:04

3 Answers3

1

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Oh, that is bad. Perhaps I could create just dummy files for all system header and use it as my system include path... – Bernd Jun 25 '20 at 14:56
  • Do you have such a tool? (I need only include processing - I use macros only if there is no other way ...) – Bernd Jun 25 '20 at 14:58
  • @BerndBaumanns Like one of the suggestions in second part of the answer? No. – eerorika Jun 25 '20 at 14:59
1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

(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"
Bernd
  • 2,113
  • 8
  • 22