WCHAR_T definition

Status
Not open for further replies.

WMXZ

Well-known member
from compiler output and tests, it seems that on Teensy WCHAR_T is defined as 32 bit.
to compile SW to be used with exFAT, one way is t define WCHAR_T as 16 bit (after all exFAT is Microsoft and wondows uses 16 bit wchar_t)

I did this by adding a compiler flag to boards.txt :" -fshort-wchar "

However, now the complier (loader) generates warnings of this type

Code:
warning: .... uses 2-byte wchar_t yet the output is to use 4-byte wchar_t; use of wchar_t values across objects may fail

To me it seems that system libraries use a 32 bit wchar_t definition

what is the best way to handle this?
Using the " -fshort-wchar " compile switch?
Are there specific reasons for Teensy arm toolchan to be compiled without "-fshort-wchar "?
 
Perhaps because the teensy is a 32Bit system :)

I think the better solution would be to use an other type..
 
I wouldn't use wchar_t for UTF16. With C++11, you have char16_t. Otherwise, there is uint16_t/unsigned short.

There is no WCHAR_T on Teensy, only wchar_t exists. The distinction is important, since there are libraries using their own variations of WCHAR_T / WCHAR.

The standard GCC arm-none-eabi toolchains (the Teensy toolchain is from there) are all built with 32-bit wchar_t.
 
I wouldn't use wchar_t for UTF16. With C++11, you have char16_t. Otherwise, there is uint16_t/unsigned short.

There is no WCHAR_T on Teensy, only wchar_t exists. The distinction is important, since there are libraries using their own variations of WCHAR_T / WCHAR.

The standard GCC arm-none-eabi toolchains (the Teensy toolchain is from there) are all built with 32-bit wchar_t.

emphasis (WCHAR_T) is my typo

Reading the Web, of course everyone says not to use wchar_t for UTF16, but tell this the original SW programmer.

on the other side 4 byte wchar_t is Linux while 2 byte wchar_t is windows and may be AIX..
Also the -fshort-wchar exist for a reason.

Now going on with my problem:

The problem is with the L"text" macro, which on Teensy seems to generate stings with 4 bytes or UTF32 (on Compiler level)
BUT I need strings with 2 bytes (UTF16)

Is there a macro for arm gcc that generates 16 bit strings?

Where can I find these macros and the connected meaning?

Edit: OK I learned L is not a macro but a prefix for the compiler,
So if L is for 32 but characters, what is the prefix for 16 bit characters?
 
Last edited:
Is there a macro for arm gcc that generates 16 bit strings?
With C++11, you have (with GCC, you can use UTF8 encoding for your source files and in your string literals):

const char16_t* str_utf16 = u"My String";
const char* str_utf8 = u8"My String";
 
Thanks
got it

Code:
  char16_t *text1 = u"1234";
  char32_t *text2 = U"1234";
  for(int ii=0;ii<10;ii++) Serial.printf("%x ",*((char*)text1+ii)); Serial.println("");
  for(int ii=0;ii<20;ii++) Serial.printf("%x ",*((char*)text2+ii)); Serial.println("");

generates
Code:
31 0 32 0 33 0 34 0 0 0 
31 0 0 0 32 0 0 0 33 0 0 0 34 0 0 0 0 0 0 0

If one knows the right questions, one always gets the right answer

thanks again
 
BTW, the situation with regards to aliasing is really bad. Per C++ standard 3.9.1/5:
Type wchar_t is a distinct type [...]. [...] Types char16_t and char32_t denote distinct types [...]

The implication of that is that casting between string variations (e.g. unsigned short / char16_t / short version of wchar_t) is undefined behavior and your code may get miscompiled. The only safe way to move to a different type is to use memcpy.
 
Status
Not open for further replies.
Back
Top