stdint.h use question


is there a namespace alias I need to use in order to reference the fields in stdint.h?

I included stdint.h in a program I'm using, and I'm trying to set a variable like this.

 
const int MAX_SMALL = UINT8_MAX;


This doesn't compile though. It tells me UINT8_MAX was not declared in this scope. I guess I don't understand how it could not be unless I need to be using a special namespace.
I'm not having trouble with gcc.

However in g++, must include -std=c++0x compiler option.
Without namespace, but #include <cstdint> as header
There's no namespace, it's a C99 standard header file. If you have stdint.h, you'll have UINT8_MAX.
In my stdint.h (taken somewhere on the internet for visualC++ 2005), i have this condition before the limits definitions :

#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS)

Try defining __STDC_LIMIT_MACROS in the preprocessor options maybe?

Edit :
In an other stdint.h for a DSP, i found this :
1
2
3
4
5
/* 
   According to footnotes in the 1999 C standard, "C++ implementations
   should define these macros only when __STDC_LIMIT_MACROS is defined
   before <stdint.h> is included." 
*/
Last edited on
You can't just copy system header files around. Either your environment supports it or it doesn't. If it doesn't you can make up definitions that are right for your environment.

When you copy system files, you're likely to get constants that are not correct for your environment.

To my knowledge, there is no stdint.h for VS2005.
So I ended up having to do the define just like bartoli said. The one kicker was I had to do the define at the very top of my program, because there were other files that specifically undefined it before that unless I put it there.

I now moved it in as one of my CPPFLAGS in the overall Makefile to make sure it gets included all the time.
Dealing with these things in a portable way always gives me pause for thought.

As a rule, I tend to put such systems specific things in a file, say defs.h. You then include defs.h first. If you're using a stdafx.h, it'd go in there.

The code might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifdef _MSC_VER
	#if _MSC_VER <= 1500
		// No stdint
		#ifndef STDINT_TYPES
		typedef unsigned short	uint16_t;
		typedef unsigned long	uint32_t;
		// ... other stdint types
		#define STDINT_TYPES
		#endif

		// No nullptr
		#ifndef NULLPTR
		#define nullptr		0
		#define NULLPTR
		#endif

		// ... other defines
	#endif
#endif 

Last edited on
Topic archived. No new replies allowed.