The ISO C standard (currently C11) has this to say about the content of the __FILE__
predefined macro:
__FILE__
: The presumed name of the current source file (a character string literal).
And that's about it. There is no mandate on the format of the content so I suspect a implementation could probably get away with setting it to "some file I found in the /src tree"
and still claim conformance.
So it's basically up to the implementation as to what it puts in there. You'll need to investigate specific implementations to see how they handle it. The gcc
compiler, for example, uses the file exactly as you specified on the command line so, if you want the full path, it's the command line you'll have to change, something like:
gcc -o myexec $(pwd)/myexec.c
It's interesting to note that gcc
seems to do the opposite for included files. When you use:
#include "myheader.h"
the __FILE__
macro is set to the full expansion of the header file.
If you have an implementation that doesn't set __FILE__
in the manner you need, there's nothing stopping you from creating your own with something like:
dodgycc -o myexec -DMY_FILE_NAME=$(pwd)/myexec.c myexec.c
(where the -D
option of the dodgycc
compiler defines a preprocessor token to be what you need).