Gcc:强制编译器默认使用无符号字符

2022-01-12 00:00:00 gcc char unsigned c++

由于当 unsigned 限定符不存在时,C++ 中 char 的性质是依赖于编译器的,是否有一个参数可以传递给 GCC,它会 强制将所有char编译为unsigned?

Since the nature of a char in C++ is compiler-dependent when the unsigned qualifier is not present, is there an argument I could pass on to GCC which would force all chars to be compiled as unsigned?

推荐答案

你要找的标志是-funsigned-char.

来自文档:

-funsigned-char

char类型是无符号的,如unsigned char.

Let the type char be unsigned, like unsigned char.

每种机器都有一个默认的 char 应该是什么.默认情况下类似于 unsigned char 或默认情况下类似于 signed char.

Each kind of machine has a default for what char should be. It is either like unsigned char by default or like signed char by default.

理想情况下,可移植程序在依赖于对象的签名时应始终使用 signed charunsigned char.但是许多程序被编写为使用纯 char 并期望它被签名,或者期望它是未签名的,这取决于它们被编写的机器.此选项及其反选项使您可以使此类程序以相反的默认值运行.

Ideally, a portable program should always use signed char or unsigned char when it depends on the signedness of an object. But many programs have been written to use plain char and expect it to be signed, or expect it to be unsigned, depending on the machines they were written for. This option, and its inverse, let you make such a program work with the opposite default.

类型 char 总是与 signed charunsigned char 中的每一个不同的类型,即使它的行为总是一样那两个.

The type char is always a distinct type from each of signed char or unsigned char, even though its behavior is always just like one of those two.

-fsigned-char

char类型被签名,就像signed char一样.

Let the type char be signed, like signed char.

注意,这等价于-fno-unsigned-char,是-funsigned-char的否定形式.同样,选项 -fno-signed-char 等效于 -funsigned-char.

Note that this is equivalent to -fno-unsigned-char, which is the negative form of -funsigned-char. Likewise, the option -fno-signed-char is equivalent to -funsigned-char.

这只影响charwchar_t 等类型不受影响.

This only impacts char; types like wchar_t are unaffected.

相关文章