Page 1 of 1

Can't compile if functions are in a separate file

PostPosted: Mon Dec 19, 2016 4:40 pm
by yawn
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

PostPosted: Tue Dec 20, 2016 12:51 pm
by Sorunome
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: Select all
int add(int x, int y){
    return x+y;
}

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

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

PostPosted: Tue Dec 20, 2016 4:33 pm
by yawn
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

PostPosted: Tue Dec 20, 2016 8:32 pm
by Sorunome
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

PostPosted: Wed Dec 21, 2016 12:32 pm
by yawn
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

PostPosted: Wed Dec 21, 2016 1:01 pm
by Sorunome
You might want to start using header file syntax and then #include "myclass.h"