Go Down

Topic: ino or cpp? (Read 957 times) previous topic - next topic

KeithRB

My clock program has gotten big enough that I am going to need to split up the source files. Is it better to put my functions in .cpp or .ino files?

Tom Carpenter

If all you want to do is split the code across multiple files (not planning on making classes or whatever) then you are best using .ino as these are all merged into a single file at compile time.
~Tom~

KeithRB

Thanks, I forgot to say no classes, or .h's required.

Nick Gammon

Personally I would use .cpp files because .ino files are concatenated together, I think in alphabetic order. There is no guarantee that that order is going to be what you want.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
There is no guarantee that that order is going to be what you want.

It shouldn't matter, since almost no one sees the .cpp file anyway. The function prototypes and header files are added the .cpp created by merging all the .ino files. The order of functions shouldn't matter, since there is no need to ensure forward references only.

KeithRB

What about global variables? for those it *does* matter!

PaulS

Quote
What about global variables? for those it *does* matter!

No, it doesn't. A global variable is one that is not defined in a function. Before setup() or after loop() makes no difference.

KeithRB

A global is only seen from the point where it is defined to the end of the file. I just created an
int afterSetup;

between setup() and loop() and put an
afterSetup = 99; in setup and received an:
"afterSetup" not declared in this scope error.

Nick Gammon

A test shows that if you create a .ino file with a name starting in "z", and putting your global variables in that, actually works because the IDE seems to load:


  • The main file first

  • Other files in the same directory



So provided all your global variables were in the first file you would be OK. But if you were trying to be really organized and only have (say) comments in the main file, you might fall into the trap.

My objection to the "concatenate source files together" is that it isn't very C-like. Usually a file is a compilation unit.

I think you might hit subtle problems, for example if you had a static variable, you might think that the variable's scope was only the file it is in, but with concatenated source files its scope is longer than is obvious.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

SirNickity

This is on my short list list of complaints with the Arduino IDE.  That concatenation thing is meant to make life simple, but it's so PFM that it introduces a chaos factor unless you learn all the subtle gotchas -- which makes it far more difficult than just teaching proper (deterministic!) file management, IMO.  I don't even so much mind the auto function prototypes and includes, but the tab combining is atrocious -- and the source of several bugs trying to derive IDE line numbers from the generated source, etc.

I've tried a couple multi-tab projects and eventually gave up.  IMO, you're better off either creating a library of functions or just using a single huge file.

Go Up