Can't compile if functions are in a separate file

Understanding the language, error messages, etc.

Can't compile if functions are in a separate file

Postby yawn » 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?
yawn
 
Posts: 32
Joined: Tue Dec 23, 2014 1:54 pm

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

Postby Sorunome » 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: 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);
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

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

Postby yawn » 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)
yawn
 
Posts: 32
Joined: Tue Dec 23, 2014 1:54 pm

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

Postby Sorunome » 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
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

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

Postby yawn » 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!
yawn
 
Posts: 32
Joined: Tue Dec 23, 2014 1:54 pm

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

Postby Sorunome » Wed Dec 21, 2016 1:01 pm

You might want to start using header file syntax and then #include "myclass.h"
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 14 guests

cron