95

I get "unknown type name 'uint8_t'" and others like it using C in MinGW.

How can I solve this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RobotRock
  • 4,211
  • 6
  • 46
  • 86

4 Answers4

184

Try including stdint.h or inttypes.h.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 2
    I still get the error for uint32_t, but including stdint.h did solve the others. – RobotRock Jan 21 '12 at 13:31
  • 1
    For the former, to avoid a warning, `warning: incompatible implicit declaration of built-in function ‘printf’`, it may be required to explicitly add `include ` (before `#include `). – Peter Mortensen Apr 26 '21 at 13:25
24

To use the uint8_t type alias, you have to include the stdint.h standard header.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ouah
  • 142,963
  • 15
  • 272
  • 331
  • To avoid a warning, `warning: incompatible implicit declaration of built-in function ‘printf’`, it may be required to explicitly add `#include ` (before `#include `). – Peter Mortensen Apr 26 '21 at 13:45
8

To be clear: If the order of your #includes matters and it is not part of your design pattern (read: you don't know why), then you need to rethink your design. Most likely, this just means you need to add the #include to the header file causing problems.

At this point, I have little interest in discussing/defending the merits of the example, but I will leave it up as it illustrates some nuances in the compilation process and why they result in errors.


You need to #include the stdint.h before you #include any other library interfaces that need it.

Example:

My LCD library uses uint8_t types. I wrote my library with an interface (Display.h) and an implementation (Display.c).

In display.c, I have the following includes.

#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include <Display.h>
#include <GlobalTime.h>

And this works.

However, if I rearrange them like so:

#include <string.h>
#include <avr/io.h>
#include <Display.h>
#include <GlobalTime.h>
#include <stdint.h>

I get the error you describe. This is because Display.h needs things from stdint.h, but it can't access it because that information is compiled after Display.h is compiled.

So move stdint.h above any library that needs it and you shouldn't get the error any more.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LanchPad
  • 257
  • 3
  • 10
  • 22
    That's just poor design, `Display.h` should contain an `#include `. Don't rely on the including file to include things for you. That's what header guards are here for. – Jerska Jul 26 '14 at 01:32
  • That's a bit incomplete. Can you expand or provide a reference on why includes should not be in the source file? My includes don't provide types needed by the functions the library provides, so I wouldn't think they need to be in the header file. – LanchPad Jul 30 '14 at 16:04
  • As soon as you use anything from any header in any file, you just include that file in your source. I did not say you should include all the files in the header (well I did, but edited long before you answered). In my comment, I only point out how bad design it is to have to include a file before another one in order to make it work. It wont save you any space or whatever because you will need to make this include each time before your file. If your `Display.h` needs `stdint.h`, it is not normal to not put the include directly in it. – Jerska Jul 31 '14 at 08:01
  • "My includes don't provide types needed by the functions the library provides" : Your answer and the error you described said the exact opposite. – Jerska Jul 31 '14 at 08:02
  • Specifically, the goal of the module is to let me use a display simply by including the 'Display.h' interface in the program. As no function exposed by the interface implements the 'stdint.h' library, it seems a little messy to expose 'stdint.h' by including it in the interface? Is there a better way to avoid that? – LanchPad Jul 31 '14 at 17:50
  • Can you clarify a bit ? In your answer, you state "This is because Display.h needs things from stdint.h". But in your comments, you seem to be saying the opposite. When you say no function exposed uses it, then you must use it in non-exposed functions (static) ? Then why are these functions declared in your "Display.h" ? – Jerska Jul 31 '14 at 17:54
  • 3
    Sorry, I was unclear. No function provided by 'Display.h' NEEDS 'stdint.h'. The functions can also accept types defined in 'Display.h' itself. As it is not necessary for a program implementing 'display.h' to also implement 'stdint.h' I don't think it would be good to expose the 'stdint.h' library without the programmer explicitly typing '#include ' to do so. – LanchPad Jul 31 '14 at 18:31
  • Well, then you're absolutely right to do so. But I don't get why you got an error then. – Jerska Jul 31 '14 at 18:34
  • http://www.embedded.com/design/prototyping-and-development/4023876/Modular-Programming-in-C It was a while back, but I believe this is the tutorial I followed when I was designing my libraries. – LanchPad Jul 31 '14 at 18:47
  • Answer is correct, but bad programming practice is offered as an example, so -1. If you get an error when you re-order your include statements, then you're not following good practices. – Greg Schmit Jan 29 '18 at 01:39
  • If there isn't an explicit `#include ` before `#include ` there may be a warning, `warning: incompatible implicit declaration of built-in function ‘printf’`. – Peter Mortensen Apr 26 '21 at 13:56
0

I had to include "PROJECT_NAME/osdep.h" and that includes the OS-specific configurations.

I would look in other files using the types you are interested in and find where/how they are defined (by looking at includes).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John b
  • 1,338
  • 8
  • 16