C Extending the preprocessor to another object-like macro

I have code like the following (mixed C / C ++ application)

#include <stdint.h> #define BUFFER_SIZE UINT16_MAX 

I expected BUFFER_SIZE be (65535) as UINT16_MAX defined in stdint.h, but instead the compiler complains that UINT16_MAX not defined. Obviously, macro expansion does not occur as we would like.

I could just identify it myself (65535), but I would like to know why this is not working.

Reply to a couple of comments:

  • My compiler supports type uint16_t and UINT16_MAX defined in stdint.h

  • One person mentioned in the definition of __STDC_LIMIT_MACROS - I tried to determine this before including stdint.h in the action.

ANSWER

So it was __STDC_LIMIT_MACROS , but more complicated.

  • #define was in the header file (including stdint.h)
  • I had #define __STDC_LIMIT_MACROS in this file before stdint.h was included
  • I included this header file in another source file. This other source file is also #include stdint.h and did this before including my header. Therefore, when stdint.h was first turned on __STDC_LIMIT_MACROS not defined

My solution was to simply add -D__STDC_LIMIT_MACROS to my compiler arguments.

+4
source share
2 answers

As you seem to be using C ++, you can do:

 #define __STDC_LIMIT_MACROS 

From /usr/include/stdint.h recent Debian:

 /* The ISO C99 standard specifies that in C++ implementations these macros should only be defined if explicitly requested. */ #if !defined __cplusplus || defined __STDC_LIMIT_MACROS ... /* Maximum of unsigned integral types. */ # define UINT8_MAX (255) # define UINT16_MAX (65535) # define UINT32_MAX (4294967295U) # define UINT64_MAX (__UINT64_C(18446744073709551615)) 
+6
source

If you include the C header from a C ++ 03 file, you need to define __STDC_LIMIT_MACROS before you include the header; otherwise, it cannot define these macros.

If you are using C ++ 11 then include the C ++ <cstdint> header instead.

+3
source

Source: https://habr.com/ru/post/1487501/


All Articles