1

Suppose I have a program

main.c

#include "file.h" 
#include <stdio.h>
int main()
{
//Code to found the included path
}

gcc -I /local main.c

How can I found the included path of header file inside this program Now their can be 3 included path

  1. current directory
  2. ENV set in the Path VARIABLE or other
  3. Directory included with -I option

Please provide a way to get this inside the same program.

Echilon
  • 10,064
  • 33
  • 131
  • 217
duck
  • 2,483
  • 1
  • 24
  • 34
  • 1
    http://stackoverflow.com/questions/13079650/how-can-i-find-the-header-files-of-the-c-programming-language-in-linux/13080356#13080356 – Sankar Mani Nov 07 '12 at 05:10
  • That question is about the user finding out where the include files were found, he wants the program itself to get the info. – Barmar Nov 07 '12 at 05:17
  • I don't think C provides this kind of introspection mechanism. There are multiple include files, which one should be "the included path"? BTW, the PATH environment variable isn't used to find include files. – Barmar Nov 07 '12 at 05:19
  • There is really no reason why you should need the include paths inside an application. – Daniel Kamil Kozar Nov 07 '12 at 06:26
  • @Daniel Kamil Kozar I need that kind of info as i am using that in my programm. – duck Nov 07 '12 at 07:00
  • 1
    @user1471175: if you explain _why_ you need that, maybe people can find ways to do what you want. As it is, it's really unclear what you'd do with that information at runtime. – Mat Nov 07 '12 at 07:03
  • @Barmer Their must be any mechanism for that? – duck Nov 07 '12 at 07:09
  • @Mat I have some header files in that path, which i need to parse in my programm. In a more clear way you can say that a programm need to parse all the linked header files which were used in the compilation of the programm and i dont want the path to be static (or preassumed as can be written to a file), I want all this in a dynamic way. Is their any way it can be done? – duck Nov 07 '12 at 07:12

1 Answers1

2

For the include files that you could edit you can use the __FILE__ macro. It makes the preprocessor insert the full file's name like /the/directory/filename.

Just add the follow line to you header:

static const char MyIncludeFileName[] = __FILE__;

If you do not refer to MyIncludeFileName (from the code which includes the header) the compiler might issue a warning that MyIncludeFileName is declared but not used. To tell the compiler be quiet about this do the followings:

static const char MyIncludeFileName[] __attribute__ ((unused)) = __FILE__;
alk
  • 69,737
  • 10
  • 105
  • 255