Sunday 15 July 2012

c - Can I rely on the include guards in standard headers -



c - Can I rely on the include guards in standard headers -

i want know if can rely on particular definitions of include guards in standard headers. if on scheme visual studio can see, example, stdint.h has _stdint defined. question if can rely on #define other compilers too. basically, safe me this:

#ifndef _stdint typedef char int8_t; #endif

i know might stupid. why not include stdint.h say? i'm writing code may deployed embedded targets, might not happy including standard c headers. however, want able utilize them when available. therefore, want able take in file has int main() whether want include stdint.h or not.

you can rely on standard library header declare (and no more than) specified declare. means that:

if feature optional, standard specify feature-test macro can check #if or #ifdef, tell if available on platform. e.g. threads.h optional in c11, standard specifies __stdc_no_threads__ defined 1 if header not available.

if feature not optional, should able assume present! if isn't present, compiler isn't conforming language version, , you'd improve hope stuff explicitly spelled out in documentation, because you're implementation-defined territory.

many features provided macros, or associated macro definitions; can hence test existence of such features independent of whole header.

so case of stdint.h, there no feature test macro provided because header not optional. if isn't present, compiler ought documenting that, , not claiming standard compliance. however, features within stdint.h optional, , hence testable individually associated macros.

for instance, exact-width integer types described in 7.20 (int8_t , on) require associated macro definitions describing maximum , minimum values (and these macros not defined if types not available), can test specific availability of int8_t testing whether int8_max defined after including header.

c include-guards

No comments:

Post a Comment