【发布时间】:2026-01-04 00:55:02
【问题描述】:
我得到了错误
‘CHAR_WIDTH’ undeclared
当我尝试编译这个简单的程序时:
#include <stdio.h>
#include <limits.h>
int main()
{
printf("CHAR_BIT = %d\n", CHAR_BIT);
printf("CHAR_WIDTH = %d\n", CHAR_WIDTH);
return (0);
}
与
gcc ./show_char_width.c -o show_char_width
和gcc:GNU C17(Ubuntu 8.3.0-6ubuntu1)版本8.3.0(x86_64-linux-gnu)编译的GNU C版本8.3.0,GMP版本6.1.2,MPFR版本4.0.2,MPC版本1.1.0,isl版本isl-0.20-GMP, 内核:5.0.0-37-generic。
如here 所述,CHAR_WIDTH 应在包含在我的程序中的limits.h 中定义。那么为什么会出现这个错误呢?
使用-v 选项,我发现将在这些目录中搜索该库:
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/8/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
/usr/lib/gcc/x86_64-linux-gnu/8/include-fixed 包含一个limits.h,其中包括来自同一目录的syslimits.h,而后者又包括下一个limits.h,据我了解应该位于 /usr/include 目录中。
CHAR_WIDTH 宏确实在这些文件中定义,但在某些超出我实际知识的情况下。
到目前为止我发现的条件是:
/* The integer width macros are not defined by GCC's <limits.h> before
GCC 7, or if _GNU_SOURCE rather than
__STDC_WANT_IEC_60559_BFP_EXT__ is used to enable this feature. */
#if __GLIBC_USE (IEC_60559_BFP_EXT)
# ifndef CHAR_WIDTH
# define CHAR_WIDTH 8
# endif
和:
#ifdef __STDC_WANT_IEC_60559_BFP_EXT__
/* TS 18661-1 widths of integer types. */
# undef CHAR_WIDTH
# define CHAR_WIDTH __SCHAR_WIDTH__
这就是为什么我需要你的帮助。
注意:对于 A.5.1 中描述的所有其他宏,我都会遇到相同的错误,特别是:SCHAR_WIDTH、INT_WIDTH、LONG_WIDTH 等。
【问题讨论】:
-
``` /* 在 GCC 7 之前,GCC 的
没有定义整数宽度宏,或者如果使用 _GNU_SOURCE 而不是 STDC_WANT_IEC_60559_BFP_EXT 来启用此功能. */ #if __GLIBC_USE (IEC_60559_BFP_EXT) # ifndef CHAR_WIDTH # 定义 CHAR_WIDTH 8 # endif``` -
@LP:可能有些OP不理解-->“超出我的实际知识。”
-
我用 /usr/include/limits.h 中的一些代码编辑了帖子
-
编辑后。定义
__STDC_WANT_IEC_60559_BFP_EXT__或通过命令行传递它 -
@pliski 你在
#define之前是#include吗?