How do I use the GNU C Library version of basename()
and dirname()
?.
If you
#include <libgen.h>
for dirname
You're already getting the POSIX, not the GNU, version of basename()
. (Even if you
#define _GNU_SOURCE
As far as I know there is no conditional importing in C. Is there a gcc specific trick?
Just write it yourself and give it a different name than basename
. This GNU insistence on creating alternate non-conforming versions of standard functions that can be written in 1-3 lines is completely batty.
char *gnu_basename(char *path)
{
char *base = strrchr(path, '/');
return base ? base+1 : path;
}
This way, your program will also be more portable.
According to the man page you should do
#define _GNU_SOURCE
#include <string.h>
#include <libgen.h>
If you get the POSIX version, libgen.h is probably already included before that point. You may want to include -D_GNU_SOURCE
in the CPPFLAGS for compilation:
gcc -D_GNU_SOURCE ....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With