C语言中可用的标准预定义宏__FILE__显示文件的完整路径。有办法缩短这条路吗?我的意思是
/full/path/to/file.c
我看到
to/file.c
or
file.c
C语言中可用的标准预定义宏__FILE__显示文件的完整路径。有办法缩短这条路吗?我的意思是
/full/path/to/file.c
我看到
to/file.c
or
file.c
当前回答
#include <algorithm>
#include <string>
using namespace std;
string f( __FILE__ );
f = string( (find(f.rbegin(), f.rend(), '/')+1).base() + 1, f.end() );
// searches for the '/' from the back, transfers the reverse iterator
// into a forward iterator and constructs a new sting with both
其他回答
使用basename()函数,或者,如果是在Windows上,使用_splitpath()。
#include <libgen.h>
#define PRINTFILE() { char buf[] = __FILE__; printf("Filename: %s\n", basename(buf)); }
还可以在shell中尝试man 3 basename。
GCC 12将提供GNU C扩展宏__FILE_NAME__来获取编译文件的基名。
请参阅GCC文档,其中已经包含了这个宏:GCC -common_predefined_macros
GCC线程:Bug 42579 - [PATCH]支持获取文件basename
Try
#include <string.h>
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
对于Windows,使用“\\”而不是“/”。
我多年来一直使用@Patrick的答案。
当完整路径包含符号链接时,它有一个小问题。
更好的解决方案。
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-builtin-macro-redefined -D'__FILE__=\"$(subst $(realpath ${CMAKE_SOURCE_DIR})/,,$(abspath $<))\"'")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-builtin-macro-redefined -D'__FILE__=\"$(subst $(realpath ${CMAKE_SOURCE_DIR})/,,$(abspath $<))\"'")
为什么要用这个?
-Wno-builtin-macro-redefined to mute the compiler warnings for redefining __FILE__ macro. For those compilers do not support this, refer to the Robust way below. Strip the project path from the file path is your real requirement. You won't like to waste the time to find out where is a header.h file, src/foo/header.h or src/bar/header.h. We should strip the __FILE__ macro in cmake config file. This macro is used in most exists codes. Simply redefine it can set you free. Compilers like gcc predefines this macro from the command line arguments. And the full path is written in makefiles generated by cmake. Hard code in CMAKE_*_FLAGS is required. There is some commands to add compiler options or definitions in some more recently version, like add_definitions() and add_compile_definitions(). These commands will parse the make functions like subst before apply to source files. That is not we want.
重新定义- wno -内置宏的健壮方法。
include(CheckCCompilerFlag)
check_c_compiler_flag(-Wno-builtin-macro-redefined SUPPORT_C_WNO_BUILTIN_MACRO_REDEFINED)
if (SUPPORT_C_WNO_BUILTIN_MACRO_REDEFINED)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-builtin-macro-redefined")
endif (SUPPORT_C_WNO_BUILTIN_MACRO_REDEFINED)
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-Wno-builtin-macro-redefined SUPPORT_CXX_WNO_BUILTIN_MACRO_REDEFINED)
if (SUPPORT_CXX_WNO_BUILTIN_MACRO_REDEFINED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-builtin-macro-redefined")
endif (SUPPORT_CXX_WNO_BUILTIN_MACRO_REDEFINED)
记住从集合(*_FLAGS…-D__FILE__ =…)。
GCC 8现在有-fmacro prefix-map和- file-prefix-map选项:
-fmacro-prefix-map = =新老 当预处理位于旧目录下的文件时,展开__FILE__和__BASE_FILE__宏,就好像文件位于新目录下一样。这可用于将绝对路径更改为相对路径。对于new,这可以导致更可重复的构建,这些构建是独立于位置的。该选项还会在编译期间影响__builtin_FILE()。参见- file-prefix-map。
-ffile-prefix-map = =新老 当编译位于旧目录中的文件时,记录所有引用 对他们来说,编译的结果就好像文件就在里面一样 改为新建目录。指定此选项相当于 指定所有单独的-f*-prefix-map选项。可以使用这个 制作独立于位置的可重复构建。另请参阅 -fmacro prefix-map和-fdebug-prefix-map。
为- file-prefix-map (-fdebug-prefix-map)设置无效路径将中断调试,除非你告诉调试器如何映射回去。(gdb: set replace -path, vscode: "sourceFileMap")。
如果你的意图只是清理__FILE__,只需使用-fmacro prefix-map。
例子: 所以对于我的Jenkins构建,我将添加- file-prefix-map=${WORKSPACE}/=/,并删除本地开发包安装前缀。
不幸的是- file-prefix-map和-fmacro prefix-map选项仅在GCC 8以后可用。例如,对于GCC 5,我们只有-fdebug-prefix-map,它不影响__FILE__。