8

I'm working with legacy embedded C code which defines the types uint8_t, uint16_t and uint32_t in a header file using the typedef keyword.

For discussion, let us say the file typedefs.h contains these definitions.

In my new C source module, I include stdint.h. I also include other header files which include typedefs.h somewhere in the hierarchy. As expected, the compiler complains about multiple defined symbols.

I would like to modify the legacy file typedefs.h so that it only declares the uint*_t types if either stdint.h is not included or better if the uint*_t types are not defined.

My understanding is that #ifndef cannot be used since typedef is not a preprocessor directive.

So how do I tell the compiler to not define the uint*_t if they already exist (or if the stdint.h is already included)?

Note: this would be easy if the C specification defined standard include guard definitions for the header files.

FWIW, I am using Green Hills compiler, 4.24, for an ARM9 processor.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154

3 Answers3

7

I beleive that the stdint.h should also be defining a macro for the limits of the types that it defines. You should be able to test for those using a #ifdef and the like.

#ifndef UINT32_MAX
  typdef ... uint32_t;
  #define UINT32_MAX ...
  ...
#endif

Edit: Originally used UINT32_MIN, but as Jens Gustedt poited out this is the one combination of signed/unsigned and min/max that doesn't occur.

torak
  • 5,684
  • 21
  • 25
  • 1
    Hm, `UINT32_MIN` doesn't exist. `UINT32_MAX` or `INT32_MIN` – Jens Gustedt Sep 07 '10 at 20:40
  • @Jens: The two following seem to suggest that such definitions are required 1) http://www.opengroup.org/onlinepubs/000095399/basedefs/stdint.h.html 2) http://linux-documentation.com/en/man/man0p/stdint.h.html – torak Sep 08 '10 at 15:46
  • 1
    yes, I know, but my point is that it doesn't require a MIN for an unsigned type, doesn't it, since this is always 0, anyhow. – Jens Gustedt Sep 08 '10 at 15:49
  • @Jens: Ahhh, the light downs. Thanks! – torak Sep 08 '10 at 15:57
  • Looking for UINT32_MAX only tells you if limits.h has been included, not stdint.h. Sometimes people include the one without the other. – Bill Jun 13 '15 at 18:10
5

Just fix the legacy header to always include stdint.h to get these types, remove the duplicate definitions, and provide a drop-in file stdint.h for broken systems that lack it.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
0

If you're on a UNIX system, then you should back-up a step and use a configuration package like autoconf(1) or automake(1). It's designed to handle problems like this.

Steve Emmerson
  • 7,702
  • 5
  • 33
  • 59
  • This is an embedded system that doesn't use Unix nor Linux, but ThreadX. Also, it was coded before `stdint.h` became standard. – Thomas Matthews Sep 07 '10 at 20:38
  • @Thomas: You probably run the Green Hills compiler on an OS other than ThreadX, such as Linux or Windows, right? Very few embedded compilers are self-hosting. That said, autoconf/automake sound like overkill for this situation, and they solve a different problem: "how do I make my code compile on multiple platforms whether they have stdint.h or not?", not "how do I make my new code that uses stdint.h stop conflicting with this old code that defines some of the same types?" – bk1e Sep 08 '10 at 06:02