gpt4 book ai didi

gcc - “INTMAX_MAX”未在此范围内声明

转载 作者:行者123 更新时间:2023-12-04 23:49:39 25 4
gpt4 key购买 nike

编译一个简单的代码片段时:test.cpp :

#include <stdint.h>
#include <stdio.h>
int main()
{
intmax_t max = INTMAX_MAX;
printf("%jd", max);
return 0;
}

我收到此错误:
[huqiu@101 ~]$ g++ -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:5: error: ‘INTMAX_MAX’ was not declared in this scope

我所做的:

编译器信息:
[huqiu@101 ~]$ which g++
/usr/bin/g++

和版本:
[huqiu@101 ~]$ g++ -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

我检查了包含路径:
[huqiu@101 ~]$ gcc -o test.o test.cpp -v

...

COLLECT_GCC_OPTIONS='-o' 'test.o' '-v' '-mtune=generic'
/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/cc1plus -quiet -v -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -auxbase test -version -o /tmp/ccjZyOY4.s
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/x86_64-redhat-linux
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/backward
/usr/local/include
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include
/usr/include
End of search list.

GNU C++ (GCC) version 4.4.7 20120313 (Red Hat 4.4.7-4) (x86_64-redhat-linux)
compiled by GNU C version 4.4.7 20120313 (Red Hat 4.4.7-4), GMP version 4.3.1, MPFR version 2.4.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9beacdb5724f8325308f81d8bbfbf884
test.cpp: In function ‘int main()’:
test.cpp:5: error: ‘INTMAX_MAX’ was not declared in this scope

还有依赖:
[huqiu@101 ~]$ gcc -M test.cpp
test.o: test.cpp /usr/include/stdint.h /usr/include/features.h \
/usr/include/sys/cdefs.h /usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h \
/usr/include/bits/wchar.h /usr/include/stdio.h \
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include/stddef.h \
/usr/include/bits/types.h /usr/include/bits/typesizes.h \
/usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h \
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include/stdarg.h \
/usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h

我确定 stdint.h存在于 /usr/include :
[huqiu@101 ~]$ cat /usr/include/stdint.h |grep INTMAX_MAX
# define INTMAX_MAX (__INT64_C(9223372036854775807))
# define UINTMAX_MAX (__UINT64_C(18446744073709551615))

gcc -E
[huqiu@101 ~]$ gcc -E test.cpp | grep INTMAX_MAX
intmax_t max = INTMAX_MAX;

我该怎么办?

一切看起来都不错。我在其他gcc版本较低的机器上编译成功,如 4.2.x .

我想可能有一些重要的事情我把它们弄错了。

任何帮助和提示都会很棒。提前致谢 :)

最佳答案

嗯,首先,您正在将代码编译为 C++。由于这些宏源自 C99,理论上它们最终会与现有 C++ 定义的相同名称发生冲突。出于这个原因,GCC 编译器安全地运行:它希望您在这些宏定义可用之前显式请求它们。

在我的 GCC 安装中,宏定义 protected

#if (!defined __cplusplus || __cplusplus >= 201103L \
|| defined __STDC_LIMIT_MACROS)

由于您显然是将代码编译为 C++11 之前的 C++,因此您必须通过定义 __STDC_LIMIT_MACROS 来请求这些宏。之前包括 stdint.h .但与此同时,如果您在 -std=c++11 中运行编译器,您的代码将“按原样”编译。模式。

另见 What do __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS mean?

关于gcc - “INTMAX_MAX”未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25561677/

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