'uint32_t' does not name a type
Asked Answered
O

10

113

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?

Orion answered 17/6, 2012 at 5:23 Comment(4)
Look for stdint.h or <cstdint> headers. That type is (as I understand it) part of C99 but didn't make it into C++.Crayfish
Did you #include <stdint.h>? 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?Bahaism
@Bahaism I checked the Makefile and there were no std options.Orion
@user667810: So that defaults to GNU extensions and C++98 mode.Bahaism
P
196

You need to include stdint.h

 #include <stdint.h>
Pennant answered 17/6, 2012 at 5:37 Comment(8)
The "proper" C++ header would be cstdint.Bonn
Note, in my case the problem was actually that the include boost/cstdint.hpp was not found. yum install boost-devel fixed my case.Dotted
@Bonn shouldn't the cstdint.h be included inside an extern "C" { } block?Sacrilegious
@Bonn 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.Av
What about C with GCC?Mclane
@Mclane - same thing. <stdint.h> will work in both C and C++ with any compiler.Pennant
@Sacrilegious all C standard library headers include extern "C" wrappers in conditional compilation for defined __cplusplus. You would only need that is using some extraordinarily old headers.Moderator
@Bonn 'proper' is not that helpful if supporting legacy code where you want int32_t in the global namespace as would have been the case when C++98 was current so did not include C99 headers. You could include cstdint, and then issue multiple using directives, but stdint.h is the path of least resistance.Moderator
M
44

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>.

Musa answered 17/6, 2012 at 5:35 Comment(2)
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.Orion
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.Musa
Y
12

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!

Yajairayajurveda answered 13/3, 2014 at 14:35 Comment(0)
L
8

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.

Locule answered 21/8, 2012 at 0:39 Comment(1)
Needed these for Kernel Driver (C), It compiles! ThanksTigerish
L
3

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>
Litigable answered 18/9, 2015 at 5:35 Comment(1)
What does this have to do with opencv? This is a very general error that happens in many circumstances.Distaste
V
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

Vareck answered 11/2, 2013 at 3:46 Comment(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).Municipal
S
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;
Sirrah answered 5/4, 2016 at 12:15 Comment(2)
@Sirrah You still need to #include the correct header before you can access the type.Ephemerality
Yes, the #include is needed. I did not say it is not. But since in my case it was a #include <cstdint>, 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;Sirrah
U
0

The best practice for C++ code is to include this file:

#include <cstdint>

And use the integer types like this:

std::uint32_t

uint32_t is not guaranteed by the C++ standard to be available in #include <cstdint>, but std::uint32_t is.

This is the same for std::size_t, std::int32_t, std::uint16_t, etc.

Unduly answered 25/11, 2023 at 8:27 Comment(0)
S
-1

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..

Strephon answered 7/10, 2019 at 4:30 Comment(1)
This is not a portable solution. And why would you include 4 lines when you could just include a single one in the form of #include <stdint.h> which is shorter and more portable and better in every way?Uyekawa
M
-6

You need to include iostream

#include <iostream>
using namespace std;
Magyar answered 12/10, 2019 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.