C/C++编译器支持 __FILE_NAME__ 获取文件名,不显示文件路径

 

 

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.)

 

4

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

 

原文链接: https://www.cnblogs.com/sinferwu/p/16500913.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C/C++编译器支持 __FILE_NAME__ 获取文件名,不显示文件路径

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/190499

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午4:12
下一篇 2023年2月12日 下午4:12

相关推荐