public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* again: sizeof(wchar_t)
@ 2004-04-24  9:21 Artem B. Bityuckiy
  2004-04-25 17:53 ` llewelly
  2004-04-26 11:45 ` Eljay Love-Jensen
  0 siblings, 2 replies; 7+ messages in thread
From: Artem B. Bityuckiy @ 2004-04-24  9:21 UTC (permalink / raw)
  To: gcc-help

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 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.

What should I do?

Thanks.

P.S. Please, CC to me if answer...

-- 
Best Regards,
Artem B. Bityuckiy,
St.-Petersburg, Russia.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: again: sizeof(wchar_t)
  2004-04-24  9:21 again: sizeof(wchar_t) Artem B. Bityuckiy
@ 2004-04-25 17:53 ` llewelly
  2004-04-25 20:47   ` Florian Weimer
  2004-04-26 11:45 ` Eljay Love-Jensen
  1 sibling, 1 reply; 7+ messages in thread
From: llewelly @ 2004-04-25 17:53 UTC (permalink / raw)
  To: Artem B. Bityuckiy; +Cc: gcc-help

"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.)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: again: sizeof(wchar_t)
  2004-04-25 17:53 ` llewelly
@ 2004-04-25 20:47   ` Florian Weimer
  2004-04-26 11:32     ` Artem B. Bityuckiy
  0 siblings, 1 reply; 7+ messages in thread
From: Florian Weimer @ 2004-04-25 20:47 UTC (permalink / raw)
  To: llewelly; +Cc: Artem B. Bityuckiy, gcc-help

llewelly@xmission.com writes:

> 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.)

autoconf has macros which can determine compile-time constants even
when cross-compiling.

-- 
Current mail filters: many dial-up/DSL/cable modem hosts, and the
following domains: atlas.cz, bigpond.com, di-ve.com, netscape.net,
postino.it, tiscali.co.uk, tiscali.cz, tiscali.it, voila.fr.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: again: sizeof(wchar_t)
  2004-04-25 20:47   ` Florian Weimer
@ 2004-04-26 11:32     ` Artem B. Bityuckiy
  0 siblings, 0 replies; 7+ messages in thread
From: Artem B. Bityuckiy @ 2004-04-26 11:32 UTC (permalink / raw)
  To: gcc-help; +Cc: Florian Weimer, llewelly

 > llewelly@xmission.com writes:
 >
 >This sounds like a newlib bug to me. You should report it.
Probably yes ...

> llewelly@xmission.com writes:
> 
>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.
You are right, I'm working with cross-compiling environment.

 > Florian Weimer wrote:
 >
> autoconf has macros which can determine compile-time constants even
> when cross-compiling.
>
Hmm, this is the variant. I didn't known that. Theoretically we can 
determine sizeof wchar_t without running on target board... I'll try to 
find these autoconf macros.

Thanks guys.

-- 
Best Regards,
Artem B. Bityuckiy,
St.-Petersburg, Russia.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: again: sizeof(wchar_t)
  2004-04-24  9:21 again: sizeof(wchar_t) Artem B. Bityuckiy
  2004-04-25 17:53 ` llewelly
@ 2004-04-26 11:45 ` Eljay Love-Jensen
  2004-04-26 18:25   ` Artem B. Bityuckiy
  1 sibling, 1 reply; 7+ messages in thread
From: Eljay Love-Jensen @ 2004-04-26 11:45 UTC (permalink / raw)
  To: Artem B. Bityuckiy, gcc-help

Hi Artem,

Use:
#if WCHAR_BITSIZE == 16
blah blah blah
#elif WCHAR_BITSIZE == 32
blah blah blah
#else
#error Unsupported/unknown wchar_t bitsize.
#endif

When you are compiling your code with 32-bit wchar_t:
g++ -DWCHAR_BITSIZE=32 foo.cpp

When you are compiling your code with 16-bit wchar_t:
g++ -DWCHAR_BITSIZE=16 foo.cpp

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: again: sizeof(wchar_t)
  2004-04-26 11:45 ` Eljay Love-Jensen
@ 2004-04-26 18:25   ` Artem B. Bityuckiy
  2004-04-26 22:50     ` Graham Nash
  0 siblings, 1 reply; 7+ messages in thread
From: Artem B. Bityuckiy @ 2004-04-26 18:25 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: gcc-help

Eljay Love-Jensen wrote:
> Hi Artem,
> 
> Use:
> #if WCHAR_BITSIZE == 16
> blah blah blah
> #elif WCHAR_BITSIZE == 32
> blah blah blah
> #else
> #error Unsupported/unknown wchar_t bitsize.
> #endif
> 
> When you are compiling your code with 32-bit wchar_t:
> g++ -DWCHAR_BITSIZE=32 foo.cpp
> 
> When you are compiling your code with 16-bit wchar_t:
> g++ -DWCHAR_BITSIZE=16 foo.cpp

The specific is that I'm upgrading Newlib itself. I'm adding locale and 
some C99 features support to it (Newlib isn't fully C99 complient). This 
is bad way to use such hack. This is like MAX_INT macro. Do you imagine, 
for example, glibc requires you to compile it with make 
CFLAGS+=-DMAX_INT=32? (this is just analogy) :-)
I hope there is some other way exists...

Thanks.


-- 
Best Regards,
Artem B. Bityuckiy,
St.-Petersburg, Russia.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: again: sizeof(wchar_t)
  2004-04-26 18:25   ` Artem B. Bityuckiy
@ 2004-04-26 22:50     ` Graham Nash
  0 siblings, 0 replies; 7+ messages in thread
From: Graham Nash @ 2004-04-26 22:50 UTC (permalink / raw)
  To: Artem B. Bityuckiy; +Cc: Eljay Love-Jensen, gcc-help

I have built a GCC cross-compiler (Solaris and linux to our odd 
platform) and this automatically defines __WCHAR_TYPE__ which is used as 
the basis for a kind of definition of wchar_t:
    typedef __WCHAR_TYPE__ __gwchar_t
(see inttypes.h). It appears to default to "int" so can be used in a 
sizeof(expression).

I'm using 3.2.2 so it may have all gotten changed since then.

--g

Artem B. Bityuckiy wrote:

> Eljay Love-Jensen wrote:
>
>> Hi Artem,
>>
>> Use:
>> #if WCHAR_BITSIZE == 16
>> blah blah blah
>> #elif WCHAR_BITSIZE == 32
>> blah blah blah
>> #else
>> #error Unsupported/unknown wchar_t bitsize.
>> #endif
>>
>> When you are compiling your code with 32-bit wchar_t:
>> g++ -DWCHAR_BITSIZE=32 foo.cpp
>>
>> When you are compiling your code with 16-bit wchar_t:
>> g++ -DWCHAR_BITSIZE=16 foo.cpp
>
>
> The specific is that I'm upgrading Newlib itself. I'm adding locale 
> and some C99 features support to it (Newlib isn't fully C99 
> complient). This is bad way to use such hack. This is like MAX_INT 
> macro. Do you imagine, for example, glibc requires you to compile it 
> with make CFLAGS+=-DMAX_INT=32? (this is just analogy) :-)
> I hope there is some other way exists...
>
> Thanks.
>
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-04-26 22:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-24  9:21 again: sizeof(wchar_t) Artem B. Bityuckiy
2004-04-25 17:53 ` llewelly
2004-04-25 20:47   ` Florian Weimer
2004-04-26 11:32     ` Artem B. Bityuckiy
2004-04-26 11:45 ` Eljay Love-Jensen
2004-04-26 18:25   ` Artem B. Bityuckiy
2004-04-26 22:50     ` Graham Nash

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).