0

I am looking to print a list of absolute paths to a specific header file used by gcc without compiling anything (or specifying a particular compile target, like with gcc -M). E.g., the following would print a list of absolute paths with all matches for limits.h found in the appropriate directories:

gcc <flag/command> limits.h

A (non-)solution that returns only working header files is to simply create a check.c, #include all desired header names there, and run gcc -M check.c.

Another solution, (albeit using bash stuff) is found in this answer and here.


Related questions that didn't quite have what I want:

0

1 Answer 1

1

It looks like you could use this answer and a little shell scripting to do what you want; e.g:

$ gcc -xc -E -v - < /dev/null 2>&1 |
  sed -n '/#include <...>/,/End of search list/ {/#include/d; /End/d; p};' |
  xargs -IPATH find PATH -name limits.h
/usr/lib/gcc/x86_64-redhat-linux/14/include/limits.h
/usr/include/c++/14/tr1/limits.h
/usr/include/linux/limits.h
/usr/include/limits.h

Or alternatively, maybe something like this:

$ echo '#include <limits.h>' | gcc -E - | grep '/limits.h' | cut -f2 -d'"' | sort -u
/usr/include/limits.h
/usr/include/linux/limits.h
/usr/lib/gcc/x86_64-redhat-linux/14/include/limits.h
Sign up to request clarification or add additional context in comments.

4 Comments

This gets the job done on Linux! Preferably looking for something just using gcc (in part for portability to MacOS & Windows, also to avoid having to macro a version of this sucker on all my machines).
This will probably work just fine on MacOS, too. I don't know what sort of common Unix tools are typically included with gcc on Windows, but things like grep, sed, and cut are pretty common.
result on MacOS is sed: 1: "/#include <...>/,/End o ...": extra characters at the end of p command, could be a little thing but not sure how to troubleshoot that
I'll have access to a MacOS system later this evening so I can check. Meanwhile, try the second solution I posted.

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.