Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC, Unicode and __FUNCTION__

Tags:

c++

gcc

unicode

I'm trying to make my project compile under GCC (Visual Studio compiles it flawlessly).

I have a custom assert function which throws a wstring message. A part of it is the _ _FUNCTION__ macro, which I "unicodize" using the WIDEN macro from MSDN

#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)

It compiles okay in MSVC, but it prints this in GCC:

error: ‘L__FUNCTION__’ was not declared in this scope

The only solution I could come with is to convert the contents of __FUNCTION __ to wstring on runtime using mbstowcs, but I would like to find a compile-time way to do it.

Thanks for help.

like image 290
Matěj Zábský Avatar asked Mar 25 '10 20:03

Matěj Zábský


People also ask

What is __ Pretty_function __?

The identifier __PRETTY_FUNCTION__ holds the name of the function pretty printed in a language specific fashion. These names are always the same in a C function, but in a C++ function they may be different. For example, this program: extern "C" { extern int printf (char *, ...

How do I Undefine a macro in Makefile?

Macros can be undefined from the command line using the /U option, followed by the macro names to be undefined.

Which GCC option Undefine a preprocessor macro?

4. Which gcc option undefines a preprocessor macro? Explanation: None.


1 Answers

In GCC __FUNCTION__ is a non-standard extension. To quote: GCC Online Docs

In GCC 3.3 and earlier, in C only, __FUNCTION__ and __PRETTY_FUNCTION__ were treated as string literals; they could be used to initialize char arrays, and they could be concatenated with other string literals. GCC 3.4 and later treat them as variables, like __func__. In C++, __FUNCTION__ and __PRETTY_FUNCTION__ have always been variables.

So adding L on the front of __FUNCTION__ is just going to turn it into L__FUNCTION__ which is probably undefined.

like image 64
Dipstick Avatar answered Oct 05 '22 21:10

Dipstick