问题
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
回答1:
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__
and format
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.
回答2:
If you're using gcc this macro should help:
__BASE_FILE__
Here's a helpful list
来源:https://stackoverflow.com/questions/27493937/base-file-name-from-file