如何在C中不使用__FILE__打印文件名[复制]

时间:2021-09-06 21:41:55

This question already has an answer here:

这个问题在这里已有答案:

I want to print the filename that is being used in my project. But if I use __FILE__, it prints entire path along with the file name which is long in my case and disturbs the log indents.

我想打印我的项目中使用的文件名。但是如果我使用__FILE__,它会打印整个路径以及文件名,这在我的情况下很长,并且会干扰日志缩进。

Can anyone please help how I can print just the file name using a common parameter across my project.

任何人都可以帮助我如何在我的项目中使用通用参数打印文件名。

2 个解决方案

#1


2  

In your C code, you might use instead of __FILE__ the expression basename(__FILE__); however, this has the disadvantage of calling basename at every occurrence.

在您的C代码中,您可以使用表达式basename(__ FILE__)来代替__FILE__;但是,这样做的缺点是每次都会调用basename。

Alternatively, compile path/foo.c with a command like

或者,使用类似命令编译path / foo.c

gcc -Wall -c -g -DBASE_FILE=\"foo\" path/foo.c

(you could have a generic make rule giving that)

(你可以有一个通用的制作规则给出)

then use BASE_FILE instead of __FILE__ in your code.

然后在代码中使用BASE_FILE而不是__FILE__。

With GCC, you could use __BASE_FILE__ instead of __FILE__

使用GCC,您可以使用__BASE_FILE__而不是__FILE__

#2


-1  

Consider using basename. I don't think the preprocessor gives you much more than __FILE__.

考虑使用basename。我认为预处理器不会给你提供比__FILE__更多的东西。

#1


2  

In your C code, you might use instead of __FILE__ the expression basename(__FILE__); however, this has the disadvantage of calling basename at every occurrence.

在您的C代码中,您可以使用表达式basename(__ FILE__)来代替__FILE__;但是,这样做的缺点是每次都会调用basename。

Alternatively, compile path/foo.c with a command like

或者,使用类似命令编译path / foo.c

gcc -Wall -c -g -DBASE_FILE=\"foo\" path/foo.c

(you could have a generic make rule giving that)

(你可以有一个通用的制作规则给出)

then use BASE_FILE instead of __FILE__ in your code.

然后在代码中使用BASE_FILE而不是__FILE__。

With GCC, you could use __BASE_FILE__ instead of __FILE__

使用GCC,您可以使用__BASE_FILE__而不是__FILE__

#2


-1  

Consider using basename. I don't think the preprocessor gives you much more than __FILE__.

考虑使用basename。我认为预处理器不会给你提供比__FILE__更多的东西。