-1

I am writing a cross-platform library for my own use. The library can be used in linux kernel, NT kernel, and many other environments, so I don't want to be dependent on any headers or libraries, even if they are standard headers. So, I cannot include stdint.h to use int32_t.

If I use VC++, I can typedef __int32 MY_INT32;, because __int32 is built in the compiler.

Is there a counterpart of __int32 in GCC?

My current practice is:

typedef signed char        KTL_INT8;
typedef signed short       KTL_INT16;
typedef signed int         KTL_INT32;
typedef signed long long   KTL_INT64;
typedef unsigned char      KTL_UINT8;
typedef unsigned short     KTL_UINT16;
typedef unsigned int       KTL_UINT32;
typedef unsigned long long KTL_UINT64;

static_assert(1 == sizeof(KTL_INT8),  "sizeof(KTL_INT8) != 1");
static_assert(2 == sizeof(KTL_INT16), "sizeof(KTL_INT16) != 2");
static_assert(4 == sizeof(KTL_INT32), "sizeof(KTL_INT32) != 4");
static_assert(8 == sizeof(KTL_INT64), "sizeof(KTL_INT64) != 8");
xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • 6
    Why can't you use ``? It is the way the compiler provides you with access to types of known sizes. You can write your own analogue of it (`#include `?) and have it define the types that `` would define. – Jonathan Leffler Jun 19 '14 at 14:01
  • @JonathanLeffler, consider developing a linux kernel module, there is no stdint.h included in the standard linux kernel headers. – xmllmx Jun 19 '14 at 14:04
  • 3
    The kernel probably doesn't provide `` because it must be provided by the language (compiler) even in a freestanding environment. See ISO/IEC 9899:1999, §4 Conformance: _A conforming freestanding implementation shall accept any strictly conforming program that does not use complex types and in which the use of the features specified in the library clause (clause 7) is confined to the contents of the standard headers ``, ``, ``, ``, ``, ``, and ``._ A kernel module should be able to use those 7 headers regardless. – Jonathan Leffler Jun 19 '14 at 14:08
  • So how does the kernel do it? They must need known-size ints all over the place? – AShelly Jun 19 '14 at 14:08
  • 1
    similar http://stackoverflow.com/questions/269614/gcc-fixed-size-integers – phuclv Jun 19 '14 at 14:10
  • Incidentally, ISO/IEC 9899:2011 adds `` and `` to the mandated list for a conforming freestanding implementation. – Jonathan Leffler Jun 19 '14 at 14:12
  • 2
    @xmllmx You should provide information about the issue you're actually trying to tackle, rather than theorizing in a comment. *Are* you building a Linux kernel module? In that case there may be a specific solution for you. If not, what are you doing so that people can help you? – Dan Fego Jun 19 '14 at 14:13
  • @DanFego, I am writing a cross-platform library for my own use. The library can be used in linux kernel, NT kernel, and many other environments, so I don't want to be dependent on any headers or libraries, even if they are standard headers. – xmllmx Jun 19 '14 at 14:22
  • @xmllmx Standard headers are exactly made for the purpose of not tying you to a specific implementation or environment? – pmr Jun 19 '14 at 14:29
  • 1
    In that case, you have all the fun of reinventing the wheel. Please ensure the corners are somewhat rounded; it makes for a smoother ride. And ignoring what the compiler tells you about itself (by ignoring those core headers) is probably not the best way to go. – Jonathan Leffler Jun 19 '14 at 14:29
  • if you really need to define it your own, try this http://stackoverflow.com/questions/1546510/declaring-fixed-size-integer-typedef-in-standard-c?rq=1 – phuclv Jun 19 '14 at 14:47
  • Your defines are bad, `1 == sizeof(KTL_INT8)` doesn't make sure that the type has 8 bits because CHAR_BIT may be 9, 12, 32 or any number >= 8. The typedefs in the link I provide is much better, or you can try `#if sizeof(int) == 4 && CHAR_BIT == 8 typedef unsigned short int uint32_t; #elif sizeof(long) == 4 && CHAR_BIT == 8...#endif` – phuclv Jun 20 '14 at 02:49

2 Answers2

1

I don't think so, no.

I looked at GCC's extensions documentation but didn't find anything other than __int128.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

This may vary with GCC versions, but in the recent few I've checked you'll have

#include <stdint-gcc.h>

This is a generated file you'll find in /usr/lib/gcc/<target>/<version>/include/ (where host and version will be specific to your GCC. It appears to use macros in the form __INT32_TYPE__ to represent the types itself.

(I suspect these are defined as attributes that map to GCC's internal type system from the machine description files, but it's been too long since I've played with GCC source.)

Rup
  • 33,765
  • 9
  • 83
  • 112