Getting base name of the source file at compile time

后端 未结 14 2232
有刺的猬
有刺的猬 2020-12-14 10:07

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<

14条回答
  •  独厮守ぢ
    2020-12-14 10:24

    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__.

提交回复
热议问题