header-filesarduino-ide

include own header file in arduino's ide


I want to include a header file that I created in my main Arduino code. unfortunately upon compilation I get the error message, that the header file could not be found.

I use the Arduino IDE 2.0.3 on windows 10 without major issues so far.

Here is, what i did so far to my best of knowledge and various websites I used as a guide did the steps 1 to 3 apparently working (i.e. compiling | its my first time trying to use my own header file btw):

  1. create working main sketch
  2. create class in main (also working)
  3. create own file (.h) in same folder as main
    • #include <file.h> and #include "file.h" both produce file not found error
  4. files have read only (rw-r--r--) permission for group and other (seemed sufficient to me)
  5. restarting ide ...
  6. create file.h one directory-level up or down

so, this is my strip down code producing the error and the error message:

filesetup:

../project1/project/
project.ino  classfile.h

project.ino:

#include <classfile.h>

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("hello world!");
  delay(1000);
}

classfile.h:

#ifndef MY_CLASS_H
#define MY_CLASS_H

#include <Arduino.h>

//usefull class

#endif

Using the Arduino IDE 2.0.3: Copilation error:

Using board 'nano' from platform in folder: C:\Users\x\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Using core 'arduino' from platform in folder: C:\Users\x\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Detecting libraries used...
"C:\\Users\\x\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Users\\x\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\x\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\eightanaloginputs" "C:\\Users\\x\\AppData\\Local\\Temp\\arduino-sketch-24B7CD866BBD71D5C48AF128C6D936B9\\sketch\\project.ino.cpp" -o nul
Alternatives for classfile.h: []
ResolveLibrary(classfile.h)
  -> candidates: []
C:\Users\x\Desktop\project\project.ino:1:10: fatal error: classfile.h: No such file or directory
 #include <classfile.h>
          ^~~~~~~~~~~~~
compilation terminated.

exit status 1

Compilation error: classfile.h: No such file or directory

What do I have to do, to fix my problem?


Solution

  • I found out that the file has to end with a .hpp (the common c++ header file ending that convetion was implemented rather hard in the arduino ide) also the include must use "" instead of <> so:

    classfile.hpp can be included using #include "classfile.hpp" for successful validation and compliation of my code.

    If by chance you know if this can be altered in the ide somewhere, please let me know.