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?

2
  • 1
    #include <stdint.h>
    – Juraj
    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?
    – ocrdu
    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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.