Is there a macro to get the basename and dirname of __FILE__?

Christophe Lyon christophe.lyon@linaro.org
Wed May 19 10:21:45 GMT 2021


On Fri, 14 May 2021 at 11:08, Jonathan Wakely via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> On Fri, 14 May 2021 at 01:01, Martin Sebor wrote:
> >
> > 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
>
> There is a patch to add one from 2010, but it never got properly
> submitted to the mailing list as far as I know:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42579

It happens that we had a very similar patch in the stm32 toolchain,
I've posted it here:
https://gcc.gnu.org/pipermail/gcc-patches/2021-May/570725.html

Thanks,

Christophe


More information about the Gcc-help mailing list