Gcc: force compiler to use unsigned char by default(Gcc:强制编译器默认使用无符号字符)
问题描述
由于当 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 char 或 unsigned 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 char 或 unsigned 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.
这只影响char;wchar_t 等类型不受影响.
This only impacts char; types like wchar_t are unaffected.
这篇关于Gcc:强制编译器默认使用无符号字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Gcc:强制编译器默认使用无符号字符
- STL 中有 dereference_iterator 吗? 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01
- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
