4

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

Zhani Baramidze
  • 1,407
  • 1
  • 13
  • 32

2 Answers2

4

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.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • This is the right answer: It is more of a `glibc` issue than a GCC version issue. – jxh Nov 03 '17 at 18:07
  • I tried enabling gnu++11 but didn't work, but more surprisingly /usr/include/limits.h has # define LLONG_MAX 9223372036854775807LL I checked with g++ -M and this file does get included – Zhani Baramidze Nov 03 '17 at 22:17
  • but I think manually defining those things with -D like __ GNUC __ or other stuff may mess up lot of other things – Zhani Baramidze Nov 03 '17 at 22:18
  • @ZhaniBaramidze If you can't use `std::numeric_limits`, then try and move the affected code to `.c` files and compile with `gcc -std=gnu99` instead. You can combine C files with C++ files in the same project without issues. Just make sure to wrap your C header declarations (the C `.h` files) inside `extern "C" { ... }` blocks, as shown in this question: https://stackoverflow.com/questions/3789340/combining-c-and-c-how-does-ifdef-cplusplus-work so that you can call your C functions from C++ code. – Nikos C. Nov 03 '17 at 22:30
  • how did people at time when debian4 was the latest debian get maximum value of long long :D I mean long long's existed already, but they had to write function to get max value manually? – Zhani Baramidze Nov 04 '17 at 00:49
  • @ZhaniBaramidze Debian 4 was released in 2007. C++11 is from 2011. So yeah, you couldn't use C99 features in C++ at the time. – Nikos C. Nov 04 '17 at 01:50
3

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>

thomas
  • 330
  • 3
  • 10