again: sizeof(wchar_t)

llewelly@xmission.com llewelly@xmission.com
Sun Apr 25 17:53:00 GMT 2004


"Artem B. Bityuckiy" <abityuckiy@yandex.ru> writes:

> Hello guys. I have already asked this
> (http://gcc.gnu.org/ml/gcc-help/2004-03/msg00350.html). May be this
> time I'll be lucky and get more answers...
> 
> I'm writing some code that depends on sizeof(wchar_t). If
> sizeof(wchar_t) > 2 I'm doing one things, if sizeof(wchar_t) = 2 -
> another things. I need to determine sizeof(wchar_t) in
> precompiler. How can I do this?
> 
> My logic is: sizeof(wchar_t) is given by gcc. I can use
> --with-target-short-wchar gcc's configure script option and gcc will
> generate 2-byte wchar_t variables. Therefore, there must be some
> predefined GCC's macro using which I can determine the size of
> wchar_t. Right?
> 
> The code for which I need to know sizeof(wchar_t) is written for
> Newlib (small standard C library). As I understand situation with
> wchar_t is similar to situation with other basic C types - they are
> defined with help of gcc's stddef.h or limits.h.
> 
> llewelly advised me to use WCHAR_MAX macro.
> 
> I tried to use something like this:
> 
> #if WCHAR_MAX > 0xFFFF
> blablabla
> #else
> blablabla
> #endif
> 
> But to my surprise I've found out that I have WCHAR_MAX==0x7FFFFFFFu
> when sizeof(wchar_t)==2! I've seen into wchar.h of Newlib - there is
> something like
> 
> #ifndef _WCHAR_MAX
> #define WCHAR_MAX 0x7FFFFFFFu
> #else
> #define WCHAR_MAX _WCHAR_MAX
> #else

This sounds like a newlib bug to me. You should report it.

> 
> This is similar to defines of other limits macros. I've seen to GCC's
> headers and not found any _WCHAR_MAX reference there.
> 
> I have seen to other libc's (FreeBSD's and Glibc) - they also rely on
> _WCHAR_MAX, but _WCHAR_MAX isn't from GCC, but is from target-depended
> files (also placed in that libc) as I've understood.
> 
> Therefore I can conclude that sizeof(wchar_t) is considered as
> target-dependent. An I can't vary gcc (using or not
> --with-target-short-wchar option when compiling it)...
> 
> But I want to have one target that is built differently depending on
> sizeof(wchar_t) that is defined by GCC. How can I do this?
> 
> One obvious way is to use construction like
> 
> if (sizeof(wchar_t) > 2)
>    {
>      blablabala
>    }
> else
>    {
>      blablabla
>    }
> 
> but I want to rely on precompiler, not on compiler.

The normal solution is to make configure compile run a program which
    outputs sizeof(wchar_t), and then substitutes that value into your
    header files. I didn't suggest this before because somehow I had
    the impression you were using a cross-compiler (which means the
    configure-time program which determines sizeof(wchar_t) must be
    downloaded to your target and its output somehow uploaded back,
    something which is not always feasible.)



More information about the Gcc-help mailing list