简体   繁体   English

stdint.h与C ++不兼容?

[英]stdint.h not compatible with C++?

I tried to compile a source code package, and found version-specific issues. 我尝试编译源代码包,并发现了特定于版本的问题。

When I do that on my computer, everything goes well; 当我在计算机上执行此操作时,一切都会顺利进行。 but when I compile it on another computer, it produces a lot of claims that INT32_MAX is not defined. 但是当我在另一台计算机上编译它时,会产生很多未定义INT32_MAX的声明。 Both computer runs a Debian system, and the difference is my computer uses Testing repo and has gcc 4.9, and the other computer uses Stable repo., which has slightly older gcc 4.7. 这两台计算机都运行Debian系统,不同之处是我的计算机使用测试存储库并具有gcc 4.9,另一台计算机使用稳定的存储库。该计算机具有稍旧的gcc 4.7。

Then I have a detailed look inside /usr/include/stdint.h . 然后,我在/usr/include/stdint.h进行了详细的查看。 By surprise, on the computer claiming the undefined macros, all int range macros are defined inside a non-C++ condition: 令人惊讶的是,在声明了未定义宏的计算机上,所有int范围宏都在非C ++条件内定义:

/**
 * I forget the exact definition,
 * but it looks like this:
 */
#ifndef __cplusplus ......
......
# define INT32_MIN XXX
# define INT32_MAX XXX
......
#endif

As a result, the package won't see these standard range macros, as it is a C++ project, and uses g++ to compile. 结果,该程序包将看不到这些标准范围宏,因为它是C ++项目,并且使用g ++进行编译。

Why the stdint.h of gcc 4.7 is designed like this? 为什么gcc 4.7的stdint.h是这样设计的? Is that means gcc 4.7 don't want me to use those integer ranges with C++, and gcc 4.9 allows it? 这是否意味着gcc 4.7不想让我在C ++中使用这些整数范围,而gcc 4.9允许它?

And the most important: how should I work around this? 最重要的是:我应该如何解决?

In C++ you're recommended to use std::numeric_limits #include <limits> : 在C ++中,建议您使用std :: numeric_limits #include <limits>

Usage example from cplusplus.com cplusplus.com的用法示例

// numeric_limits example
#include <iostream>     // std::cout
#include <limits>       // std::numeric_limits

int main () {
  std::cout << std::boolalpha;
  std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
  std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
  std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
  std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
  std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
  return 0;
}

Include climits or limits.h that contains sizes of integral types. 包括climits或limits.h,其中包含整数类型的大小。 This header defines constants with the limits of fundamental integral types for the specific system and compiler implementation used. 该头文件定义了常量,并限制了所使用的特定系统和编译器实现的基本整数类型。 The limits for fundamental floating-point types are defined in cfloat ( earlier float.h). 基本浮点类型的限制在cfloat(更早的float.h)中定义。 The limits for width-specific integral types and other typedef types are defined in cstdint (earlier stdint.h). 宽度特定的整数类型和其他typedef类型的限制在cstdint(更早的stdint.h)中定义。

You should include cstdint instead of stdint.h 您应该包括cstdint而不是stdint.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM