Implication of using -fshort-wchar
Asked Answered
W

2

6

While going through the file wchar.h on Mac OS X system, I found that wchar_t equivalent of str functions such as wcscpy, wcscat are poisoned when __cplusplust is not defined and max size of wchar_t is of 2 bytes (by using compiler option -fshort-wchar).

It seems that for C program, it does not allow such functions to use if -fshort-wchar is defined. I would like to know that what is the implication of using wchar_t functions when -fshort-wchar is used?

You may wonder that why I need to use -fshort-wchar. Because, I am porting an application initially written for Windows where size of wchar_t is two bytes. And that data held in wchar_t string is written on the file and also exchanged between two applications.

What is a good way to handle the variability of wchar_t on different platforms? Assumption on Windows is that wchar_t is of 16 bits.

What answered 8/3, 2013 at 4:12 Comment(0)
E
7

-fshort-wchar is not usable if you want to interact with any part of the standard library or third-party library code using the correct (32-bit) definition of wchar_t. The situations where it's viable are very limited and probably pertain mostly to freestanding-implementation/embedded stuff.

In the case of porting the Windows application you're dealing with, I think you should just do a seatch-and-replace to change these uses of wchar_t to WCHAR (which you're free to define) or char16_t (defined by C11, and you can supply your own definition pre-C11).

Ender answered 8/3, 2013 at 5:55 Comment(4)
We have typedefed wchar_t. So, changing is not an issue. Or should be use char on Mac OS X? Change the encoding of the content while writing it to the file or on sending/recieving?What
@dbasic: You're not allowed to typedef it. Doing so will result in Undefined Behavior, which means we cannot predict what will happen.Propositus
typedefed means we use typedef wchar_t Char; and we use Char everywhere rather than wchar_t directly.What
Then just change it to typedef uint16_t Char;Ender
E
2

If we go with typedef uint16_t Char;

What about String literals in the code? and the problem third party remain the same. These have to converted to wchar_t(UTF-32_ before passing.

Easing answered 3/4, 2013 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.