The standard predefined MACRO __FILE__ available in C shows the full path to the file. Is there any way to short the path? I mean instead of
/full/path/to/file.c
I see
to/file.c
or
file.c
The standard predefined MACRO __FILE__ available in C shows the full path to the file. Is there any way to short the path? I mean instead of
/full/path/to/file.c
I see
to/file.c
or
file.c
A bit late to the party, but for GCC have a look at the
-ffile-prefix-map=old=new
option:So for my Jenkins builds I will add
-ffile-prefix-map=${WORKSPACE}/=/
, and another to remove the local dev package install prefix.NOTE Unfortunately the
-ffile-prefix-map
option is only avalable in GCC 8 onwards, as is-fmacro-prefix-map
which, I think, does the__FILE__
part. For, say, GCC 5, we only have-fdebug-prefix-map
which does not (appear to) affect__FILE__
.in vs, when with /FC, FILE equals full path, without /FC FILE equals file name. ref here
just hope to improve FILE macro a bit:
#define FILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
this catches / and \, like Czarek Tomczak requested, and this works great in my mixed environment.
I have just thought of a great solution to this that works with both source and header files, is very efficient and works on compile time in all platforms without compiler-specific extensions. This solution also preserves the relative directory structure of your project, so you know in which folder the file is in, and only relative to the root of your project.
The idea is to get the size of the source directory with your build tool and just add it to the
__FILE__
macro, removing the directory entirely and only showing the file name starting at your source directory.The following example is implemented using CMake, but there's no reason it wouldn't work with any other build tools, because the trick is very simple.
On the CMakeLists.txt file, define a macro that has the length of the path to your project on CMake:
On your source code, define a
__FILENAME__
macro that just adds the source path size to the__FILE__
macro:Then just use this new macro instead of the
__FILE__
macro. This works because the__FILE__
path will always start with the path to your CMake source dir. By removing it from the__FILE__
string the preprocessor will take care of specifying the correct file name and it will all be relative to the root of your CMake project.If you care about the performance, this is as efficient as using
__FILE__
, because both__FILE__
andSOURCE_PATH_SIZE
are known compile time constants, so it can be optimized away by the compiler.The only place where this would fail is if you're using this on generated files and they're on a off-source build folder. Then you'll probably have to create another macro using the
CMAKE_BUILD_DIR
variable instead ofCMAKE_SOURCE_DIR
.Use the basename() function, or, if you are on Windows, _splitpath().
Also try
man 3 basename
in a shell.If you are using
CMAKE
with GNU compiler thisglobal
define works fine: