2

GCC's preprocessor-option "-H" prints the name of each header file used to STDERR. Is it possible to forward this output to a file, instead of letting it print to STDERR, and let GCC continue with the compilation in one step?

Something like:

gcc -H=file.h -c file.cc

I know I can use SHELL tricks to filter out this "-H"-output, but I would like to avoid that.

I just want to write the "-H"-output to a file instead of STDERR. I don't care if this output for some reason had to be mixed/interleaved with some other output, as long as the "-H"-output did not pollute STDERR and normal compilation/linking warnings/errors were still being printed to STDERR.

While GCC's developer-option "-save-temps" saves the entire preprocessor-output to a file, it does not save the "-H"-output with it.

5
  • 4
    any_command 2>file_to_redirect_stderr_to Commented Feb 21, 2016 at 14:31
  • 2
    I specifically said I wanted to avoid a solution like that. My build system can't handle SHELL commands like that. Commented Feb 21, 2016 at 14:36
  • I am unable to find anything in gcc's man page for this. Did you check gcc's man page? That should be an authoritative source. Explain why your build system "can't handle" redirection. Commented Feb 21, 2016 at 14:43
  • Looked all over for this, GCC's manpage, stackoverflow, etc. 1st: The proposed solution redirects both "-H"-output and compilation-errors to the file. A build system that relies on STDERR to report build errors would fail to detail them. 2nd: Separation of concerns. Can't GCC handle this independently of the build environment, like what SHELL variant is used, just like it does with the option -save-temps? 3rd: Our build system is outsourced. I have no control over it. It is also under constant change. It is also likely that the SHELL variant will change, since we now use the inflexible csh. Commented Feb 21, 2016 at 15:30
  • In case someone else need to forward the "-H"-output to a file and still keep compilation errors to be printed to STDERR, I came up with this csh-solution: any_command |& awk '{if(/^\./ || /^Multiple include guards/ || /^\//){print > "file_to_redirect_h_option_output_to"} else {print > "/dev/stderr"}}' Commented Feb 21, 2016 at 15:43

2 Answers 2

1
g++ -save-temps -c file.cc

GCC's developer option "-save-temps" does not rely on SHELL tricks and does not produce unwanted extra STDERR output. Instead, the GCC option produces two files (file.ii and file.s), where file.ii contains the preprocessor output.

At a later stage, e.g., independent of the build system/procedure, this file.ii can be transformed into something similar to what the "-H"-output would have produced. In a csh SHELL, you can use the following command (or something similar) to achieve this transformation:

cat file.ii | grep '^# ' | awk '{a=$0;gsub(/"/,"",a);split(a,b);if(/\" 1/){split(c,d," ");c=d[1]". ";print c b[3]}if(/\" 2/){split(c,d,". ");c=d[1]" "}}' > file.h

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Write a small wrapper around gcc to do what you want, instead of shell tricks.

  2. GCC is open source software. If something is missing, feel free to add it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.