81

I'm trying to compile a C++ software package that was written in 2007 and I'm getting this error:

error: ‘uint32_t’ does not name a type

This is happening in 64-bit Ubuntu using g++ 4.5.2. It compiles fine on 64-bit CentOS using g++ 4.1.2.

Is there an #include or a compiler flag that I'm missing? Or, should I use typedef to assign uint32_t to a size_t or maybe an unsigned int?

Toby Speight
  • 23,550
  • 47
  • 57
  • 84
rmtheis
  • 6,978
  • 9
  • 56
  • 74
  • 5
    Look for stdint.h or headers. That type is (as I understand it) part of C99 but didn't make it into C++. – Mike C Jun 17 '12 at 05:28
  • 3
    Did you `#include `? Looks like a possible bug on 64 bit Ubuntu. Also, do you have a `-std=c++98` or some such command line option for gcc? If so, can you check if it compiles fine if you use `-std=gnu++98`? – dirkgently Jun 17 '12 at 05:29
  • @dirkgently I checked the Makefile and there were no `std` options. – rmtheis Jun 17 '12 at 05:59
  • @user667810: So that defaults to GNU extensions and C++98 mode. – dirkgently Jun 17 '12 at 06:00

9 Answers9

163

You need to include stdint.h

 #include <stdint.h>
selbie
  • 82,148
  • 13
  • 83
  • 154
  • 61
    The "proper" C++ header would be `cstdint`. – paxdiablo Jun 17 '12 at 05:53
  • 1
    Note, in my case the problem was actually that the include `boost/cstdint.hpp` was not found. `yum install boost-devel` fixed my case. – snooze92 Jul 16 '14 at 11:33
  • @paxdiablo shouldn't the cstdint.h be included inside an extern "C" { } block? – StarShine Oct 26 '17 at 16:39
  • @paxdiablo the 'proper' header gave me "#error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental ..." Yeah, yeah, should upgrade, but it's an enormous old app that barely made it into 64-bit world. – brewmanz Apr 30 '18 at 21:43
  • What about `C` with GCC? – Royi Sep 02 '18 at 17:42
  • @Royi - same thing. `` will work in both C and C++ with any compiler. – selbie Sep 02 '18 at 20:48
36

You need to #include <cstdint>, but that may not always work.

The problem is that some compiler often automatically export names defined in various headers or provided types before such standards were in place.

Now, I said "may not always work." That's because the cstdint header is part of the C++11 standard and is not always available on current C++ compilers (but often is). The stdint.h header is the C equivalent and is part of C99.

For best portability, I'd recommend using Boost's boost/cstdint.hpp header, if you're willing to use boost. Otherwise, you'll probably be able to get away with #include'ing <cstdint>.

Uli Köhler
  • 11,813
  • 14
  • 55
  • 105
plasma
  • 798
  • 4
  • 8
  • This gave me `#error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.` – rmtheis Jun 17 '12 at 05:58
  • 1
    Right, as it says, cstdint is part of the new C++ standard (which was called C++0x but is not, officially, C++11. So to use that header, you have to enable the new standard in g++. Like I said, the best portable way to get these types is to use Boost or some other equivalent header, rather than relying on compiler support. – plasma Jun 17 '12 at 06:01
9

I also encountered the same problem on Mac OSX 10.6.8 and unfortunately adding #include <stdint.h> or <cstdint.h> to the corresponding file did not solve my problem. However, after more search, I found this solution advicing to add #include <sys/types.h> which worked well for me!

patti_jane
  • 2,444
  • 4
  • 17
  • 23
6

The other answers assume that your compiler is C++11 compliant. That is fine if it is. But what if you are using an older compiler?

I picked up the following hack somewhere on the net. It works well enough for me:

  #if defined __UINT32_MAX__ or UINT32_MAX
  #include <inttypes.h>
  #else
  typedef unsigned char uint8_t;
  typedef unsigned short uint16_t;
  typedef unsigned long uint32_t;
  typedef unsigned long long uint64_t;
  #endif

It is not portable, of course. But it might work for your compiler.

Daniel Lemire
  • 3,187
  • 2
  • 23
  • 22
2

if it happened when you include opencv header.

I would recommand that change the order of headers.

put the opencv headers just below the standard C++ header.

like this:

#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
user3094631
  • 285
  • 1
  • 12
1

Add the following in the base.mk file. The following 3rd line is important -include $(TOP)/defs.mk

CFLAGS=$(DEBUG) -Wall -W -Wwrite-strings 
CFLAGS_C=-Wmissing-prototypes
CFLAGS_CXX=-std=c++0x
LDFLAGS=
LIBS=

to avoid the #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options

Bohemian
  • 365,064
  • 84
  • 522
  • 658
  • 1
    The question doesn't say whether Make is being used. A more portable answer would just say which flags to pass to the compiler (and which compiler you're assuming). – Toby Speight Nov 08 '16 at 12:05
1

I had tha same problem trying to compile a lib I download from the internet. In my case, there was already a #include <cstdint> in the code. I solved it adding a:

using std::uint32_t;
Daniel
  • 336
  • 2
  • 6
  • 1
    @Daniel You still need to `#include` the correct header before you can access the type. – cbr Apr 05 '16 at 14:23
  • Yes, the `#include` is needed. I did not say it is not. But since in my case it was a `#include `, and there was no `using namespace std`, the compiler was not able to resolve the name `uint32_t`. So that is the reason I had to add the `using std::uint32_t;` – Daniel Apr 06 '16 at 17:49
0

just navigate to /usr/include/x86_64-linux-gnu/bits open stdint-uintn.h and add these lines

typedef __uint8_t uint8_t;
typedef __uint16_t uint16_t;
typedef __uint32_t uint32_t;
typedef __uint64_t uint64_t;

again open stdint-intn.h and add

typedef __int8_t int8_t;
typedef __int16_t int16_t;
typedef __int32_t int32_t;
typedef __int64_t int64_t;

note these lines are already present just copy and add the missing lines cheerss..

-1

You need to include iostream

#include <iostream>
using namespace std;