gpt4 book ai didi

c++ - C 预处理器扩展到另一个类似对象的宏

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:36 27 4
gpt4 key购买 nike

我有如下代码(混合 C/C++ 应用程序)

#include <stdint.h>
#define BUFFER_SIZE UINT16_MAX

我原以为 BUFFER_SIZE 是 (65535) 就像 UINT16_MAX 是在 stdint.h 中定义的,但编译器却提示 UINT16_MAX 不是定义。显然,宏展开并没有像我希望的那样发生。

我可以自己将其定义为 (65535),但想知道为什么这不起作用。

对一些评论的回应:

  • 我的编译器确实支持 uint16_t 类型并且 UINT16_MAX 在 stdint.h 中定义

  • 有人提到定义 __STDC_LIMIT_MACROS - 我曾尝试在包含 stdint.h 之前定义它,但没有效果。

回答

所以它是 __STDC_LIMIT_MACROS 但更复杂。

  1. #define 在头文件中(包括 stdint.h)
  2. 在包含 stdint.h 之前,我在此文件中有#define __STDC_LIMIT_MACROS
  3. 我将该头文件包含在另一个源文件中。这个其他源文件也#includestdint.h 并在包含我的标题之前这样做。因此,当首次包含 stdint.h 时,未定义 __STDC_LIMIT_MACROS

我的解决方案只是将 -D__STDC_LIMIT_MACROS 添加到我的编译器参数中。

最佳答案

因为您似乎在使用 C++,您可能会这样做:

#define __STDC_LIMIT_MACROS

来自最近的 Debian 的 /usr/include/stdint.h:

/* The ISO C99 standard specifies that in C++ implementations these
macros should only be defined if explicitly requested. */
#if !defined __cplusplus || defined __STDC_LIMIT_MACROS

...

/* Maximum of unsigned integral types. */
# define UINT8_MAX (255)
# define UINT16_MAX (65535)
# define UINT32_MAX (4294967295U)
# define UINT64_MAX (__UINT64_C(18446744073709551615))

关于c++ - C 预处理器扩展到另一个类似对象的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17239124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com