简体   繁体   中英

Predeclaration of C stdint types

When I tried to declare some types defined in stdint.h a conflict with /usr/include/bits/stdint-uintn.h:24:19 predeclaration occurs

typedef struct __uint8_t uint8_t;

Error message:

/usr/include/bits/stdint-uintn.h:24:19: error: conflicting types for ‘uint8_t’
   24 | typedef __uint8_t uint8_t;
      |                   ^~~~~~~
In file included from ../src/server/connection.c:1:
../src/server/connection.h:4:26: note: previous declaration of ‘uint8_t’ was here
    4 | typedef struct __uint8_t uint8_t;

How can I predeclare those types in my header files to avoid circular dependencies?

This is because stdint.h includes bits/stdint-uintn.h , in which

typedef struct __uint8_t uint8_t;

is defined.

You should not redefine uint8_t . To me the solution is to remove

typedef struct __uint8_t uint8_t;

from your file ../src/server/connection.c .

Is there any reason you need to redefine it yourself?

#include <stdint.h> is the correct way to declare these types. Any other approach is undefined and/or non-portable.

There can never be a circular dependency issue with standard headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM