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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
sramij
  • 4,775
  • 5
  • 33
  • 55
  • Do you mean WIDEN macro similar to the one from: http://stackoverflow.com/questions/3291047/how-do-i-print-the-string-which-file-expands-to-correctly/3291315#3291315 – VestniK Feb 04 '11 at 06:21
  • @VestniK: That only works for *VStudio*. – CristiFati Jul 17 '18 at 12:40

3 Answers3

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.
CristiFati
  • 38,250
  • 9
  • 50
  • 87
-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.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
Ian Davids
  • 37
  • 5
  • 2
    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##x – codesniffer Apr 20 '16 at 16:00
-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.

Sdaz MacSkibbons
  • 27,668
  • 7
  • 32
  • 34
  • __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 wcscopy – sramij Mar 28 '11 at 11:14