__FILE__显示带有文件名的完整路径

3

我正在使用

DPRINTF(ERROR_LEVEL,"ERROR: error found at file: %s line: %d",__FILE__,__LINE__);

像这样的it printf

ERROR: error found at file: /home/jeegar/ full path to that file/main.c line: 102

在这里,我只想打印出来

ERROR: error found at file: main.c line: 102

我只想要文件名,而不是整个路径。

我正在使用makefile运行此文件,并在其中使用此机制。

PATH_NOW              = $(shell pwd)
LIB                   =$(PATH_NOW)/../lib

每当我需要访问lib文件夹中的任何文件时,我只需在其中包含即可。
$(LIB)/main.c
1个回答

6

变更:

DPRINTF(ERROR_LEVEL,"ERROR: error found at file: %s line: %d",__FILE__,__LINE__);

to:

DPRINTF(ERROR_LEVEL,"ERROR: error found at file: %s line: %d",basename(__FILE__),__LINE__);

不要忘记 #include <libgen.h> 以避免警告! - Evert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接