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.
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.
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?
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.
Consider this simple source code:
On Solaris, with GCC 4.3.1, if I compile this using:
the output is '
x.c
' If I compile it using:then __FILE__ maps to the full path ('
/work1/jleffler/tmp/x.c
'). If I compile it using: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.
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)
Note : For my application I needed to escape the filename string like this:
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:
Hope this is useful to someone else as well :)