wide version of __FUNCTION__ on linux
Asked Answered
F

3

7

is there a way i can print __FUNCTION__ as a wide character on linux?

the trick with the WIDEN doesn't work for me, the gcc compiler prints: error: ?L_FUNCTION_? was not declared in this scope

any help? Thanks

Farquhar answered 23/1, 2011 at 13:3 Comment(2)
Do you mean WIDEN macro similar to the one from: #3291547Mneme
@VestniK: That only works for VStudio.Woodworth
W
4

7+ years later (although things seem to be the same) ...

Since you mentioned gcc, check [GNU.GCC]: Standard Predefined Macros (emphasis is mine):

C99 introduced __func__, and GCC has provided __FUNCTION__ for a long time. Both of these are strings containing the name of the current function (there are slight semantic differences; see the GCC manual). Neither of them is a macro; the preprocessor does not know the name of the current function.

Since __FUNCTION__ is not a macro (the preprocessor doesn't "know" anything about it), it will remain untouched during (outer) macro expansion, generating at the end the L__FUNCTION__ identifier, which is obviously invalid.
That's why the double macro approach works for __FILE__ (for example), but not for __FUNCTION__ (or __func__).

So, the short answer to your question is "NO" (at least not at preprocessor level). You will need to convert __FUNCTION__ "manually" (e.g. using one of the [man7]: MBSTOWCS(3) functions family).

Note: It works on VStudio, since according to [MS.Docs]: Predefined Macros (emphasis still mine):

  • __FUNCTION__ Defined as a string literal that contains the undecorated name of the enclosing function. The macro is defined only within a function.
Woodworth answered 17/7, 2018 at 12:36 Comment(0)
A
-2

It can be done using macros, you just have to understand how macros expand. To get a wide char version of the macro you need to create 2 layers of macros like so:

#define WIDE2(x) L##x
#define WIDECHAR(x) WIDE2(x)

#define WIDE_FUNCTION WIDECHAR(__FUNCTION__)

The all important piece is the L##x that appends the L character to the string constant before the compiler sees it. You can do this to __FILE__ as well using the same technique.

Antebi answered 26/8, 2014 at 18:33 Comment(1)
Works in VS 2013, but not in GCC (only tired with v4.9.3). Error msg: StringMacros.h:16:18: error: expected ‘)’ before ‘L__FUNCTION__’ #define WIDE2(x) L##xRosco
D
-3

That looks more like a typo of __FUNCTION__ than an issue with widen() or similar, at least if you pasted the exact error message.

Defant answered 23/1, 2011 at 13:13 Comment(1)
FUNCTION is a variable name instantiated by the compiler at each function in the code, this variable is being declared with ASCII representation (char*). hence, u cannot take this and convert to wide char without allocating a new wide character and copy into it using wcscopyFarquhar

© 2022 - 2024 — McMap. All rights reserved.