0

I wonder why the following minimal example sketch doesn't compile (unknown type name 'uint16_t'):

sketch_does_not_compile.ino:

#include "myheader.h"
void setup() {
 char a[]="hello world";
 uint16_t l=str_len(a);
 Serial.println(l);
}
void loop() {}

myheader.c:

uint16_t str_len(char* input_string) {
 uint16_t i=0;
 while (input_string[i++]!='0円');
 return i;
}

myheader.h:

#include "myheader.h"
uint16_t str_len(char* input_string);

Compilation fails with:

myheader.c: At top level:
myheader.c:3:1: error: unknown type name 'uint16_t'
 uint16_t str_len(char* input_string);
 ^
exit status 1
unknown type name 'uint16_t'

Using uint16_t in the .ino-file directly like in the sketch below compiles fine.

sketch_compiles.ino:

uint16_t str_len(char* input_string) {
 uint16_t i=0;
 while (input_string[i++]!='0円');
 return i;
}
void setup() {
 char a[]="hello world";
 uint16_t l=str_len(a);
 Serial.println(l);
}
void loop() {}

What am I missing?

ocrdu
1,7953 gold badges12 silver badges24 bronze badges
asked Dec 27, 2019 at 4:24
2
  • 1
    #include <stdint.h> Commented Dec 27, 2019 at 5:43
  • 1
    Why is the declaration of str_len() in the .c file and the implementation in the .h file? Commented Dec 27, 2019 at 11:03

1 Answer 1

5

Using #include <Arduino.h> in your myheader.h will also include <stdint.h> and probably solve this.

The Arduino IDE automatically includes Arduino.h in the sketch itself, but apparently not for external stuff.

answered Dec 27, 2019 at 11:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.