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条回答
Lonely孤独者°
2楼-- · 2019-01-23 06:28

You might be able to do it with template metaprogramming, but there's no built-in way to do it.

EDIT: Hm, correction. According to one page I just saw, GCC uses the path that it's given for the file. If it's given the full name, it'll embed it; if it's only given a relative one, it'll only embed that. I haven't tried it myself though.

查看更多
Evening l夕情丶
3楼-- · 2019-01-23 06:29

What does your error logging macro do? I would presume at some point the macro eventually calls a function of some kind in order to do the logging, why not have the called function strip off the path component at runtime?

#define LOG(message) _log(__FILE__, message)

void _log(file, message)
{
  #ifndef DEBUG
  strippath(file); // in some suitable way
  #endif

  cerr << "Log: " << file << ": " << message; // or whatever
}
查看更多
萌系小妹纸
4楼-- · 2019-01-23 06:29

You can assign __FILE__ to a string, and then call _splitpath() to rip the pieces out of it. This might be a Windows/MSVC-only solution, honestly I don't know.

I know you were looking for a compile-time solution and this is a run-time solution, but I figured since you were using the filename to do (presumably run-time) error logging, this could be a simple straightforward way to get you what you need.

查看更多
该账号已被封号
5楼-- · 2019-01-23 06:30

Consider this simple source code:

#include <stdio.h>
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.

查看更多
The star\"
6楼-- · 2019-01-23 06:31

Since you tagged CMake, here's a neat solution to add to your CMakeLists.txt: (copied from http://www.cmake.org/pipermail/cmake/2011-December/048281.html ). (Note : some compilers don't support per-file COMPILE_DEFINITIONS ! but it works with gcc)

set(SRCS a/a.cpp b/b.cpp c/c.cpp d/d.cpp)

foreach(f IN LISTS SRCS)
 get_filename_component(b ${f} NAME)
 set_source_files_properties(${f} PROPERTIES
  COMPILE_DEFINITIONS "MYSRCNAME=${b}")
endforeach()

add_executable(foo ${SRCS})

Note : For my application I needed to escape the filename string like this:

COMPILE_DEFINITIONS "MYSRCNAME=\"${b}\"")
查看更多
神经病院院长
7楼-- · 2019-01-23 06:32

Just got the same issue; found a different resolution, just thought I'd share it:

In a header file included in all my other files:

static char * file_bname = NULL;
#define __STRIPPED_FILE__   (file_bname ?: (file_bname = basename(__FILE__)))

Hope this is useful to someone else as well :)

查看更多
登录 后发表回答