LLONG_MAX missing for g++ 4.9.4?

Viewed 1600

I was trying to debug big this simple code:

#include "limits.h"

int main()
{
  long long a = LLONG_MAX;

  return 0;
}

If I run it just like

g++ test.cpp

I get

test.cpp: In function ‘main’:
test.cpp:5:17: error: ‘LLONG_MAX’ undeclared (first use in this function)
long long a = LLONG_MAX;

I checked the reference for this constant, which says:

LLONG_MIN, LLONG_MAX and ULLONG_MAX are defined for libraries complying with the C standard of 1999 or later (which only includes the C++ standard since 2011: C++11).

so I tried setting -std=c++11 but didn't help. Any suggestions?

P.S same code compiles fine on other machines with g++ 5.4.0

Updates: LONG_MAX is visible (but LLONG_MAX isn't), machine is debian4

2 Answers

Debian 4 is very old and comes with a glibc version that does not properly support C99. Even if you install a newer GCC version, you would still need to install a newer glibc version too. Which is probably not possible on Debian 4.

glibc is the standard C library implementation on Debian and most other Linux distributions.

If you want to use more modern C features, you should use a more modern Debian version.

You can still try to enable GNU extensions when compiling, in the hope that the glibc version you're dealing with exposes more C99 features in GNU mode:

g++ -std=gnu++11 test.cpp

Also do a search for LLONG_MAX in the system headers:

grep -r LLONG_MAX /usr/include

If that comes up empty, there's nothing you can do. If it finds something, then try to see by which macro LLONG_MAX is protected and whether you need to define that macro in your compiler flags (using the -D option).

Also try using "proper" C++ functions for this, as suggested by tpr in his/her answer. If std::numeric_limits works, then use that.

If none of that works, you really only have two options. Find a way to upgrade glibc without bricking the system, or upgrade to a newer Debian version.

In C++ you should use std::numeric_limits

#include <limits>
...
std::numeric_limits<long long>().max();

If you want the c defines, please use #include <climits>

Related