linuxgccio-redirectionc-header

When locating C program header/s (.h) program is using. Looking for a certain one. How do you grep output from gcc -H -fsyntax-only source.c?


Quite often one wants to know which header/s (path to programheader.h) your C program is depending on.
You can list all, with the command mentioned below, but what if looking for a certain one from the list?

As a programheader.h (e.g. errno.h) could be redundant, at least by name, with many versions for their particular purposes all over a filesystem, is very useful to be able to quickly find which one is using the program you are dealing with.
I found this SO post: How can I find the header files of the C programming language in Linux?, truly very useful:

gcc -H -fsyntax-only myprogram.c 

But when I tried:

gcc -H -fsyntax-only myprogram.c | grep errno.h

I got the full list again.
Or if I try an indirect way (e.g. to grep the file afterwards), for example:

gcc -H -fsyntax-only myprogram.c >> headers_myprogram.txt 
gcc -H -fsyntax-only myprogram.c | tee -a headers_myprogram.txt

I got the full list again (console) and a headers_myprogram.txt 0 bytes file in either case.


Solution

  • The main issue is: The output of gcc comes on the standard error stream. So any way to filter that will work, for example:

    gcc -H -fsyntax-only myprogram.c 2>&1 | grep errno.h
    
    gcc -H -fsyntax-only myprogram.c |& grep errno.h
    
    gcc -H -fsyntax-only myprogram.c 2> headers_myprogram.txt
    grep errno.h headers_myprogram.txt
    

    There are countless other alternatives, including scripting with your favorite scripting language.