Can you retrieve header file location at runtime?

I am wondering if it's possible to retrieve the location of a header file at runtime. For example, if I have a file named "myHeader.h" which I've saved in a folder called "Library/", is it possible through code to access the Library/ folder?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include "../../Library/myHeader.h"

  int main() {

   //Let's say I stored a file in the same folder as the header's folder, so what I want is...

  std::string pathName = "../../Library/"
  
  //But I want to plan for the possibility the header file gets moved to another location that I don't know about

  std::string pathName = GetHeaderFileLocation(); 

  //Ideally, if the path weren't changed, GetHeaderFileLocation() would return "../../Library/"

    return 0;
  }
Last edited on
Try the macro __FILE__

1
2
3
4
5
6
7
#include <iostream>

int main()
{
    std::cout << "The current file is " << __FILE__  << std::endl;
    return 0;
}
I am wondering if it's possible to retrieve the location of a header file at runtime.

Out of interest, why?

__FILE__ A string literal containing the presumed name of the source file being compiled.
http://www.cplusplus.com/doc/tutorial/preprocessor/

But this will be the location of the file at the time you compiled your exe. It won't be dynamic, which your function GetHeaderFileLocation(); (and comment above it) suggest you are hoping for.

In short, the answer to your question is no!

(That is, there is no generic mechanism. But you have a config file (or registry entry in the case of windows) which tells you app where to find the header file.)

Andy
Last edited on
Topic archived. No new replies allowed.