5

I am trying to get the absolute path of the compiled file at compile time in C++. I am aware of the __FILE__ macro - however, the macro can be evaluated to either absolute path, or a relative path, depending on the preprocessor's arguments.

I would like to ensure that my __FILE__ (or any other macro) evaluates to a full, absolute path of the file. Is there a way to reliably do it cross-platform? (I am compiling for VS2013, VS2015, GCC on ubuntu, GCC on MinGW)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Michał
  • 2,202
  • 2
  • 17
  • 33
  • 2
    pass an absolute path when compiling the file, at least when you control the build like in the makefile – Jean-François Fabre May 01 '18 at 21:51
  • 1
    What are you trying to achieve? Maybe a suitable solution would be to pass something like `-DDIR=$PWD` ? – Eugene Sh. May 01 '18 at 21:56
  • @Jean-FrançoisFabre Thanks! That works in GCC - I set up my makefiles to work that way. However in Visual Studio, VS has the control over the paths. Currently I'm using this solution to retrieve the project directory in Visual Studio: https://stackoverflow.com/a/38203437/2946480. Then, in my code I do a few `#ifdefs`, so that I can get an absolute path this way. However, it would be great to have a solution that works across the board. – Michał May 01 '18 at 22:01
  • @EugeneSh. I am trying to get the absolute path for logging. I compare the path to the GitHub repo root to get the file's path relative to the root of the repository. Could you elaborate on the `-DDIR=$PWD` flag? I could not find any information on that. – Michał May 01 '18 at 22:05
  • 2
    My guess on the `$PWD` trick is: `$PWD` is a shell variable (from `make`, probably) that has the current directory. I believe you need to add double quotes (i.e. `-DDIR=\"$PWD/\"`) and then when you use `__FILE__`, you could use `DIR __FILE__`. This is two strings like: `"/foo/"` and `"bar.c"` and the _compiler_ will [always] concatenate two abutting strings (i.e. the result is `"/foo/bar.c"`). `DIR` doesn't have to be `PWD` but could [might be better to be] the project dir as you've described – Craig Estey May 01 '18 at 23:20
  • @CraigEstey Thank you! It makes much more sense now. I thought `-DDIR` was a predefined flag. Instead `-D` is the flag, and in this context it means: `define DIR as $PWD`, correct? – Michał May 01 '18 at 23:24
  • 1
    Yes, instead of `DIR`, you could call it `MY_SUPER_DUPER_PROJECT_DIRECTORY_TOP` [if you like :-)]. Remember to do the backslashed double quotes as `DIR` should eval to a _string_ that is the equivalent of: `#define DIR "/foo/"`. When I do this sort of thing, I use `PRJDIR` or `PRJSRC` – Craig Estey May 01 '18 at 23:28
  • Does this answer your question? [\_\_FILE\_\_ macro shows full path](https://stackoverflow.com/questions/8487986/file-macro-shows-full-path) – phuclv Mar 30 '20 at 11:47
  • @phuclv I think that solves the opposite question. I'm looking for the absolute path, not the relative path. – Michał Mar 31 '20 at 20:59
  • there's already an answer in that question: [*In VC, when using /FC, `__FILE__` expands to the full path*](https://stackoverflow.com/a/41499524/995714). [How to force absolute paths for build errors output in Visual Studio](https://stackoverflow.com/q/11874086/995714) – phuclv Apr 01 '20 at 02:24
  • That's a question that only asks about Visual Studio. I was asking about various platforms here, hoping for a language sanctioned way of doing this, or at least something that works across gcc and vc. – Michał Apr 03 '20 at 18:59

0 Answers0