在此范围内未声明“INTMAX_MAX”

时间:2014-08-29 04:22:04

标签: gcc g++

编译简单代码片段时: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

我认为可能有些重要的事情让我弄错了。

任何帮助和提示都会很棒。在此先感谢:)

1 个答案:

答案 0 :(得分:7)

嗯,首先,您将代码编译为C ++。由于这些宏源自C99,因此理论上它们最终可能与现有的C ++定义冲突,因为它们具有相同的名称。由于这个原因,GCC编译器可以安全地使用它:它希望您在这些宏定义可用之前明确请求它们。

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

保护
#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?