Is there a macro to get the basename and dirname of __FILE__?
Martin Sebor
msebor@gmail.com
Thu May 13 23:01:29 GMT 2021
On 5/4/21 9:34 AM, Peng Yu via Gcc-help wrote:
> Hi,
>
> __FILE__ is the full path of the file used at compile time.
>
> $ cat a/test.c
> #include <stdio.h>
> int main() {
> puts(__FILE__);
> return 0;
> }
> $ gcc a/test.c
> $ ./a.out
> a/test.c
>
> The following discussion says there is no way to get its basename and
> dirname at compile time. Is it still the case?
>
> Could <string.h> functions be used at compile time for __FILE__ since
> __FILE__ is known at compile time anyway? Thanks.
>
> https://stackoverflow.com/questions/8487986/file-macro-shows-full-path
>
There is no macro for just the base name part of __FILE__ that I know
of but it can be computed from __FILE__ using strrchr as someone shows
in the thread:
(strrchr (__FILE__, '/') ? strrchr (__FILE__, '/') + 1 : __FILE__)
This is evaluated at compile time even without optimization.
Getting just the directory part isn't as easy.
Martin
More information about the Gcc-help
mailing list