C/C++编译器支持 __FILE_NAME__ 获取文件名,不显示文件路径
使用这个
-fmacro-prefix-map=old=new
-ffile-prefix-map=old=new
cmake中使用
add_compile_options(-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=.)
https://stackoverflow.com/questions/8487986/file-macro-shows-full-path
Recent Clang compiler has a __FILE_NAME__
macro (see here).
Builtin Macros
__BASE_FILE__
- Defined to a string that contains the name of the main input file passed to Clang.
__FILE_NAME__
- Clang-specific extension that functions similar to
__FILE__
but only renders the last path component (the filename) instead of an invocation dependent full path to that file. __COUNTER__
- Defined to an integer value that starts at zero and is incremented each time the
__COUNTER__
macro is expanded. __INCLUDE_LEVEL__
- Defined to an integral value that is the include depth of the file currently being translated. For the main file, this value is zero.
__TIMESTAMP__
- Defined to the date and time of the last modification of the current source file.
__clang__
- Defined when compiling with Clang
__clang_major__
- Defined to the major marketing version number of Clang (e.g., the 2 in 2.0.1). Note that marketing version numbers should not be used to check for language features, as different vendors use different numbering schemes. Instead, use the Feature Checking Macros.
__clang_minor__
- Defined to the minor version number of Clang (e.g., the 0 in 2.0.1). Note that marketing version numbers should not be used to check for language features, as different vendors use different numbering schemes. Instead, use the Feature Checking Macros.
__clang_patchlevel__
- Defined to the marketing patch level of Clang (e.g., the 1 in 2.0.1).
__clang_version__
- Defined to a string that captures the Clang marketing version, including the Subversion tag or revision number, e.g., “
1.5 (trunk 102332)
”. __clang_literal_encoding__
- Defined to a narrow string literal that represents the current encoding of narrow string literals, e.g.,
"hello"
. This macro typically expands to “UTF-8” (but may change in the future if the-fexec-charset="Encoding-Name"
option is implemented.) __clang_wide_literal_encoding__
- Defined to a narrow string literal that represents the current encoding of wide string literals, e.g.,
L"hello"
. This macro typically expands to “UTF-16” or “UTF-32” (but may change in the future if the-fwide-exec-charset="Encoding-Name"
option is implemented.)
GCC 12 will provide GNU C extensions macro __FILE_NAME__
to get basename of the compiled file.
See GCC documentation which already contains this macro : gcc-common_predefined_macros
GCC thread : Bug 42579 - [PATCH] support for obtaining file basename
GCC 8 now has the -fmacro-prefix-map
and -ffile-prefix-map
options:
-fmacro-prefix-map=old=new
When preprocessing files residing in directory old, expand the
__FILE__
and__BASE_FILE__
macros as if the files resided in directory new instead. This can be used to change an absolute path to a relative path by using.
for new which can result in more reproducible builds that are location independent. This option also affects__builtin_FILE()
during compilation. See also-ffile-prefix-map
.
-ffile-prefix-map=old=new
When compiling files residing in directory old, record any references to them in the result of the compilation as if the files resided in directory new instead. Specifying this option is equivalent to specifying all the individual
-f*-prefix-map
options. This can be used to make reproducible builds that are location independent. See also-fmacro-prefix-map
and-fdebug-prefix-map
.
Setting an invalid path for -ffile-prefix-map
(-fdebug-prefix-map
) will break debugging unless you tell your debugger how to map back. (gdb: set substitue-path
, vscode: "sourceFileMap"
).
If your intent is to only clean up __FILE__
just use -fmacro-prefix-map
.
Example: 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
and -fmacro-prefix-map
options are only available in GCC 8 onwards. For, say, GCC 5, we only have -fdebug-prefix-map
which does not affect __FILE__
.
https://www.codenong.com/8487986/
-fmacro-prefix-map=old=new