Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

‘INTMAX_MAX’ was not declared in this scope

Tags:

gcc

g++

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

like image 389
srain Avatar asked Mar 19 '23 15:03

srain


1 Answers

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?

like image 96
AnT Avatar answered Mar 31 '23 18:03

AnT