1

I want to include a function in my sketch I moved to a .h file.

I did it as follows:

My code:

#include "header.h"

void setup() {
  fct();
}

void loop() {

}

My header:

#include "Arduino.h"
#ifndef MYHELPERS_H
#define MYHELPERS_H

void fct() {
// stuff
}

#endif

The same process seems to work for variables but the IDE says this function in not declared in this scope...

I don't understand what I'm doing wrong...

Thanks guys!

5
  • for me it compiles. do you have the header file as tab in IDE?
    – Juraj
    Mar 20 at 5:23
  • Where exactly does your header file lie? In the same folder as the main sketch? Or in the libraries folder?
    – chrisl
    Mar 20 at 11:43
  • This is not the way you separate your project in modules. In a header file you declare what your module offers publically. The implementation goes into another C++ file and this will be compiled separately. -- Please get a good book on basic C++ and/or Arduino programming and read it. This issue is far too broad to be answered in a SE/Arduino question. Mar 20 at 14:22
  • This header file was created with the new file button in the Arduino editor and it resides in the same folder as the main sketch.
    – Spyro 999
    Mar 21 at 14:37
  • File->New, creates a new sketch
    – Juraj
    Mar 21 at 14:56
0

I found a solution somewhere else on internet...

This portion:

#ifndef MYHELPERS_H
#define MYHELPERS_H

Was not written correctly... "MYHELPERS_H" should've been replaced by the name of my file. Now it works just fine.

7
  • 2
    there is no relation between the include guard macro name and the name of the file. this answer is wrong.
    – Juraj
    Mar 21 at 14:52
  • It's weird but it fixed my problem
    – Spyro 999
    Mar 22 at 15:58
  • Maybe the guard macro name has to be the name using in the #include of the main sketch but it works now I have done this
    – Spyro 999
    Mar 22 at 16:00
  • no. #include takes a file name. and it doesn't care about the content of the file
    – Juraj
    Mar 22 at 16:06
  • 1
    I'm on a work trip right now and my pc is home
    – Spyro 999
    Mar 24 at 18:29

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