Thread: how to find the file given by #include?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    how to find the file given by #include?

    hi guys

    It's about the beginning section of every code where you give the libraries
    I need a shortcut to find some file given through #include<file.name> without searching though all header data.

    is there any way?

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your question is ambiguous.

    In code within an #include'd file, it is possible to find its filename (or, more accurately, a string that is representative of the filename) using the __FILE__ prefined macro (which is specified in the standard).

    From a source file that is #include'ing another files, each #include directive specifies the name of the file it is including. There is no way, within a source file, to write code to enumerate the files which that source file has #include'd.

    The only way is to use some other program (eg a program that searches files for a string) to externally search the source files for #include directives. Even that can be fooled, however, by conditional macro expansion (eg an #include directive surrounded by #ifdef ABC/#endif where ABC is not defined) or by the fact that the filename given to an #include directive can be the result of a macro expansion.

    I suspect your requirement is flawed (i.e. you're actually trying to do something else, and have somehow latched onto the idea of doing this in error).
    Last edited by grumpy; 03-25-2013 at 03:04 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you could do this
    Code:
    $ gcc -M foo.c
    foo.o: foo.c /usr/include/stdio.h /usr/include/features.h \
     /usr/include/x86_64-linux-gnu/bits/predefs.h \
     /usr/include/x86_64-linux-gnu/sys/cdefs.h \
     /usr/include/x86_64-linux-gnu/bits/wordsize.h \
     /usr/include/x86_64-linux-gnu/gnu/stubs.h \
     /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \
     /usr/lib/gcc/x86_64-linux-gnu/4.6.1/include/stddef.h \
     /usr/include/x86_64-linux-gnu/bits/types.h \
     /usr/include/x86_64-linux-gnu/bits/typesizes.h /usr/include/libio.h \
     /usr/include/_G_config.h /usr/include/wchar.h \
     /usr/lib/gcc/x86_64-linux-gnu/4.6.1/include/stdarg.h \
     /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \
     /usr/include/x86_64-linux-gnu/bits/sys_errlist.h \
     /usr/lib/gcc/x86_64-linux-gnu/4.6.1/include-fixed/limits.h \
     /usr/lib/gcc/x86_64-linux-gnu/4.6.1/include-fixed/syslimits.h \
     /usr/include/limits.h /usr/include/x86_64-linux-gnu/bits/posix1_lim.h \
     /usr/include/x86_64-linux-gnu/bits/local_lim.h \
     /usr/include/linux/limits.h \
     /usr/include/x86_64-linux-gnu/bits/posix2_lim.h
    Or this
    Code:
    $ gcc -E foo.c | awk '/^#/ && $3 ~ "/" { print $3 }' | sort -u
    "/usr/include/features.h"
    "/usr/include/_G_config.h"
    "/usr/include/libio.h"
    "/usr/include/limits.h"
    "/usr/include/linux/limits.h"
    "/usr/include/stdio.h"
    "/usr/include/wchar.h"
    "/usr/include/x86_64-linux-gnu/bits/local_lim.h"
    "/usr/include/x86_64-linux-gnu/bits/posix1_lim.h"
    "/usr/include/x86_64-linux-gnu/bits/posix2_lim.h"
    "/usr/include/x86_64-linux-gnu/bits/predefs.h"
    "/usr/include/x86_64-linux-gnu/bits/stdio_lim.h"
    "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h"
    "/usr/include/x86_64-linux-gnu/bits/types.h"
    "/usr/include/x86_64-linux-gnu/bits/typesizes.h"
    "/usr/include/x86_64-linux-gnu/bits/wordsize.h"
    "/usr/include/x86_64-linux-gnu/gnu/stubs-64.h"
    "/usr/include/x86_64-linux-gnu/gnu/stubs.h"
    "/usr/include/x86_64-linux-gnu/sys/cdefs.h"
    "/usr/lib/gcc/x86_64-linux-gnu/4.6.1/include-fixed/limits.h"
    "/usr/lib/gcc/x86_64-linux-gnu/4.6.1/include-fixed/syslimits.h"
    "/usr/lib/gcc/x86_64-linux-gnu/4.6.1/include/stdarg.h"
    "/usr/lib/gcc/x86_64-linux-gnu/4.6.1/include/stddef.h"
    What's the chance that you're using some other compiler, but didn't think it was worth mentioning at the time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does #include indirectly include the source file too?
    By Lord Asriel in forum C Programming
    Replies: 10
    Last Post: 11-30-2011, 08:20 AM
  2. Any tool to find all include from a given c++ file?
    By homer_3 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2011, 08:55 PM
  3. Include File
    By Sri Bharath in forum C Programming
    Replies: 2
    Last Post: 10-27-2009, 02:54 PM
  4. Replies: 3
    Last Post: 02-10-2009, 02:51 PM
  5. include file
    By enjoy in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2004, 04:06 PM