base file name from __FILE__

巧了我就是萌 提交于 2019-12-12 04:59:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!