Getting base name of the source file at compile time

后端 未结 14 2233
有刺的猬
有刺的猬 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:11

    Consider this simple source code:

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

    On Solaris, with GCC 4.3.1, if I compile this using:

    gcc -o x x.c && ./x
    

    the output is 'x.c' If I compile it using:

    gcc -o x $PWD/x.c && ./x
    

    then __FILE__ maps to the full path ('/work1/jleffler/tmp/x.c'). If I compile it using:

    gcc -o x ../tmp/x.c && ./x
    

    then __FILE__ maps to '../tmp/x.c'.

    So, basically, __FILE__ is the pathname of the source file. If you build with the name you want to see in the object, all is well.

    If that is impossible (for whatever reason), then you will have to get into the fixes suggested by other people.

提交回复
热议问题