Getting base name of the source file at compile ti

2019-01-23 05:31发布

I'm using GCC; __FILE__ returns the current source file's entire path and name: /path/to/file.cpp. Is there a way to get just the file's name file.cpp (without its path) at compile time? Is it possible to do this in a portable way? Can template meta programming be applied to strings?

I am using this in an error logging macro. I really do not want my source's full path making its way into the executable.

12条回答
Evening l夕情丶
2楼-- · 2019-01-23 06:09

I don't know of a direct way. You could use:

#line 1 "filename.c"

at the top of the source file to set the value of __FILE__, but I'm not sure that that's much better than hard coding it. or just using a #define to create your own macro.

Another option might be to pass the name from your Makefile using -D and $(shell basename $<)

Edit: If you use a #define or the -D option, you should create your own new name and not try to redefine __FILE__.

查看更多
叛逆
3楼-- · 2019-01-23 06:09

You can take __FILE__ and the strip off the part of path you don't want (programatically). If basedir satisfies your needs, then fine. Otherwise, get source dir root from your build system, and the rest should be doable.

查看更多
欢心
4楼-- · 2019-01-23 06:11

Taking the idea from Glomek, it can be automated like this:

Source file x.c

#line 1 MY_FILE_NAME
#include <stdio.h>

int main(void)
{
    puts(__FILE__);
    return(0);
}

Compilation line (beware the single quotes outside the double quotes):

gcc -DMY_FILE_NAME='"abcd.c"' -o x x.c

The output is 'abcd.c'.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-23 06:18

If you're using a make program, you should be able to munge the filename beforehand and pass it as a macro to gcc to be used in your program.

In your makefile, change the line:

file.o: file.c
    gcc -c -o file.o src/file.c

to:

file.o: src/file.c
    gcc "-D__MYFILE__=\"`basename $<`\"" -c -o file.o src/file.c

This will allow you to use __MYFILE__ in your code instead of __FILE__.

The use of basename of the source file ($<) means you can use it in generalized rules such as ".c.o".

The following code illustrates how it works.

File makefile:

mainprog: main.o makefile
    gcc -o mainprog main.o

main.o: src/main.c makefile
    gcc "-D__MYFILE__=\"`basename $<`\"" -c -o main.o src/main.c

File src/main.c:

#include <stdio.h>

int main (int argc, char *argv[]) {
    printf ("file = %s\n", __MYFILE__);
    return 0;
}

Run from the shell:

pax@pax-desktop:~$ mainprog
file = main.c
pax@pax-desktop:~$

Note the "file =" line which contains only the basename of the file, not the dirname.

查看更多
smile是对你的礼貌
6楼-- · 2019-01-23 06:18

Could be done ONLY programmatically.

maybe this is useful...

filename = __FILE__
len  = strlen(filename)

char *temp = &filename[len -1]
while(*temp!= filename)
    if(*temp == '\') break;
查看更多
beautiful°
7楼-- · 2019-01-23 06:18

You could use:

bool IsDebugBuild()
{
    return !NDEBUG;
}

Or, you could use NDEBUG in your Macro to turn on/off those file paths.

查看更多
登录 后发表回答