问题描述
只有当 __ FILE __
和 __ FILEW __
宏返回整个路径时,我才需要文件名。
我定义了以下内容:
#define __FILE_NAME_ONLY__(strrchr(__ FILE__,'\\ \\\')?strrchr(__ FILE__,'\\')+ 1:__FILE__)
我使用它如下:
#define MY_TRACE(mask,format,...)\
GlobalTrace(mask,L - [__FILE_NAME_ONLY__ L:format,__VA_ARGS__)
以下错误:
错误C2064:term不评估为带有1个参数的函数
当我尝试下面的宏:
#define __WIDE(_String)L ## _String
# define _WIDE(_String)__WIDE(_String)
如下
#define MY_TRACE(mask,format,...)\
GlobalTrace(mask,L - [_WIDE(__ FILE_NAME_ONLY__)L:format ,__VA_ARGS__)
我得到:错误C2146:语法错误: 'L'当我真的尝试使用MY_TRACE宏
我缺少什么?感谢
您依赖于字符串字面连接,除非所有的术语都不是字符串字面。 >
我假设你以前这样做:
#define MY_TRACE掩码,格式...)\
GlobalTrace(mask,L - [__FILE__ L:format,__VA_ARGS__)
如果
__ FILE __
和格式
展开为字符串文字,粘贴在一起。ABCD
与ABCD
当您替换为
__ FILE_NAME_ONLY __
时不会发生,因为它扩展为函数调用,而不是文字。I need the file name only where the
__FILE__
and__FILEW__
macros return the whole path.I defined the following:
#define __FILE_NAME_ONLY__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
I am using it as follows:
#define MY_TRACE( mask, format, ... ) \ GlobalTrace( mask, L"-[" __FILE_NAME_ONLY__ L":" format , __VA_ARGS__ )
I get the following error:
error C2064: term does not evaluate to a function taking 1 arguments
and when I try the following macros:
#define __WIDE(_String) L ## _String #define _WIDE(_String) __WIDE(_String)
as follows
#define MY_TRACE( mask, format, ... ) \ GlobalTrace( mask, L"-[" _WIDE(__FILE_NAME_ONLY__) L":" format , __VA_ARGS__ )
I get : error C2146: syntax error : missing ')' before identifier 'L' when I actually try to use the MY_TRACE macro
what am I missing? Thanks
解决方案You're depending on string literal concatenation, except that all of the terms aren't string literals.
I assume you were previously doing it like so:
#define MY_TRACE( mask, format, ... ) \ GlobalTrace( mask, L"-[" __FILE__ L":" format , __VA_ARGS__ )
If
__FILE__
andformat
expand to a string literal, the 4 strings get pasted together into one."A" "B" "C" "D"
is the same as"ABCD"
.That doesn't happen when you replace with
__FILE_NAME_ONLY__
because it expands to a function call, not a literal.这篇关于基本文件名从__FILE__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!