简体   繁体   中英

How to get the absolute path of specific header file?

I have a C file with some #include instructions of header files. I know that when I compile the C file with gcc, the compiler looks for the .h files in the paths contained in the environment variable $CPATH . How can I get the absolute path of a specific .h file? (I prefer to get the path without compilation)

If you compile with -E you will get the preprocessor output and within that all of the included files will have their full paths. eg in the following, the file 'file' will contain the preprocessed output:

gcc -E -o file file.c

This is a bit of a hack:

cpp -MD file.c | sed -n '/\.h"/s/.*"\(.*\)".*/\1/p' | sort -u

and clearly depends on the output format of cpp . (The above works for gnu cpp 3.4.6) This gives the full paths of all included files. Fun it through another filter to reduce the output to the specific header you care about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM