Switch to full style
Understanding the language, error messages, etc.
Post a reply

Can't compile if functions are in a separate file

Mon Dec 19, 2016 4:40 pm

Hello,

The compiler that comes with the Arduino IDE doesn't seem to like it when you have your functions or classes in separate files. Does it work ok with anothe IDE? How do you do to avoid this problem?

Re: Can't compile if functions are in a separate file

Tue Dec 20, 2016 12:51 pm

That is because you need to pre-declare the function also in the file you use them.
So lets say you have otherfile.ino:
Code:
int add(int x, int y){
    return x+y;
}

Then add to the top of you main.ino:
Code:
int add(int x, int y);

Re: Can't compile if functions are in a separate file

Tue Dec 20, 2016 4:33 pm

Thanks! Works fine. Classes won't though. Any other way than creating a library? (saving this link here for future reference https://www.arduino.cc/en/Hacking/LibraryTutorial)

Re: Can't compile if functions are in a separate file

Tue Dec 20, 2016 8:32 pm

You can put the class layout thing in your main.ino file, i think, and then have myclass::myfunc(int blahblahblah); in that other ino file

Re: Can't compile if functions are in a separate file

Wed Dec 21, 2016 12:32 pm

Sorunome wrote:You can put the class layout thing in your main.ino file, i think, and then have myclass::myfunc(int blahblahblah); in that other ino file

Should've tried that, thanks!

Re: Can't compile if functions are in a separate file

Wed Dec 21, 2016 1:01 pm

You might want to start using header file syntax and then #include "myclass.h"
Post a reply