0

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?

  • What compiler are you using? What are its command line arguments? – tstanisl Jan 31 '21 at 17:08
  • Try to add -std=c89 to options – tstanisl Jan 31 '21 at 17:09
  • I use gcc with `-std=c99 -g -Wall -Og` – R.M. Freeman Jan 31 '21 at 17:09
  • `typedef struct __uint8_t uint8_t;` remove `struct`? What it's doing there? Anyway, why would you ever do that? Just `include `? `to avoid circular dependencies?` ? How are "circular dependencies" related? – KamilCuk Jan 31 '21 at 17:26
  • Removing struct will just cause type to be not defined. Simply if you keep four `#include`s inside of headers you will get them included circular everywhere they occurs. To avoid this evaluate them inside of your *.c files. – R.M. Freeman Jan 31 '21 at 17:30
  • I included only `stdint.h` – R.M. Freeman Jan 31 '21 at 17:51
  • as KamilCuk said, just include `stdint.h`, there will be no circular dependencies, because there's nothing circular about that. you may have multiple `#include` statements for the same stdint.h, but that's not a problem, it already has include guards – Stefan Riedel Jan 31 '21 at 18:08

2 Answers2

2

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 ?

Jessy SIMEON
  • 23
  • 1
  • 5
1

#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.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Ok, thank you, I didn't know that. But what makes them safe in this situation, are they locked controlled by some memory management? – R.M. Freeman Jan 31 '21 at 20:54
  • @DanielFreeman I don't understand your question . Declaring types does not involve any memory management – M.M Jan 31 '21 at 21:04
  • Yes, but I don't relly understand why is it correct to re-include `stdint` any where with the header file. I see that It most do something that not-standard types does not. Isn't this bad to let a `include` predecessor inside of a header file? I thought headers should be pure and restrict to the needful declarations. – R.M. Freeman Jan 31 '21 at 21:14
  • @DanielFreeman It's not bad to include a standard header that provides declarations that your header file requires . – M.M Jan 31 '21 at 21:15
  • Ok, I think I understand now. So the `stdint` does include.. macros or declaration but not definitions, as it is just a layer over standard compiler types? – R.M. Freeman Jan 31 '21 at 21:18
  • "declaration" and "definition" are the same thing for typedefs . – M.M Jan 31 '21 at 21:34
  • Ok, right. Thank you. I will remember to not override `typedef`ed stuff. – R.M. Freeman Jan 31 '21 at 21:42