'INTMAX_MAX'未在此范围内声明 [英] ‘INTMAX_MAX’ was not declared in this scope

查看:1862
本文介绍了'INTMAX_MAX'未在此范围内声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我得到这个错误:

  [huqiu @ 101〜] $ g ++ -o test.o test.cpp 
test.cpp:函数'int main()':
test.cpp:5:错误:'INTMAX_MAX'未在此范围内声明




我所做的:




编译器信息:

  [huqiu @ 101〜] $ which g ++ 
/ usr / bin / g ++

和版本:

  [huqiu @ 101〜] $ g ++ -v 
使用内置规格。
目标:x86_64-redhat-linux
配置为:../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
线程模型:posix
gcc版本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
忽略不存在的目录/usr/lib/gcc/x86_64-redhat-linux/4.4。 7 / include-fixed
忽略不存在的目录/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../x86_64-redhat-linux/include
#include...搜索从这里开始:
#include< ...>搜索从这里开始:
/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
搜索列表结束。

GNU C ++(GCC)版本4.4.7 20120313(Red Hat 4.4.7-4)(x86_64-redhat-linux)
由GNU C编译版本4.4.7 20120313(Red Hat 4.4.7-4),GMP版本4.3.1,MPFR版本2.4.1。
GGC启发式:--param ggc-min-expand = 100 --param ggc-min-heapsize = 131072
编译器可执行校验和:9beacdb5724f8325308f81d8bbfbf884
test.cpp:In function'int main )':
test.cpp:5:错误:'INTMAX_MAX'未在此范围内声明

还有依赖关系:

  [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 / inclu de / bits / sys_errlist.h

我相信 stdint .h 存在于 / usr / include 中:

<$ p $ (__INT64_C(9223372036854775807))
#define UINTMAX_MAX(__UINT64_C(__INT64_C(9223372036854775807))
$ [$ q $ c $] $ cat /usr/include/stdint.h | grep INTMAX_MAX
# 18446744073709551615))

gcc -E $ b

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




我应该怎样做什么?






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



我认为可能有一些重要的东西让我错了。

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

解决方案

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



在我的GCC安装中,宏定义受< ($定义__cplusplus || __cplusplus> = 201103L \
||定义__STDC_LIMIT_MACROS)

$ b $ pre $

由于您明显将代码编译为C ++ 11 C ++之前版本,因此您必须通过定义 __ STDC_LIMIT_MACROS ,其中包括 stdint.h 。但同时,如果以 -std = c ++ 11 模式运行编译器,代码将按原样编译。



另请参阅什么是__STDC_LIMIT_MACROS和__STDC_CONSTANT_MACROS是什么意思?


When Compiling a simple code snippets: test.cpp:

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

I get this error:

[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


What I've done:


The compiler information:

[huqiu@101 ~]$ which g++
/usr/bin/g++

And the version:

[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)

I've checked the include path:

[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

And also the dependency:

[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

I am sure that stdint.h is existent in /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;


what should I do?


Everything looks right. I compiled the code successfully in other machine whose gcc version is lower, such as 4.2.x.

I think there may be something important I made them wrong.

Any help and tips will be great. Thanks in advance :)

解决方案

Well, first and foremost, you are compiling your code as C++. Since these macros originate from C99, they can theoretically end up conflicting with existing C++ definitions for the same names. For which reason GCC compiler plays it safe: it wants you to explicitly request these macro definitions before they become available.

In my GCC installation the macro definitions are protected by

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

Since you are apparently compiling your code as pre-C++11 C++, you have to request these macros by defining __STDC_LIMIT_MACROS before including stdint.h. But at the same time your code will compile "as is" if you run the compiler in -std=c++11 mode.

Also see What do __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS mean?

这篇关于'INTMAX_MAX'未在此范围内声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆