Extra code files

Hi, please could someone answer some basic questions I have. I’m a bit dumb when it comes to file types. We have .h files, .cpp files and in the case of the Arduino .ino files.

As I understand it .h files are header files which effectively contain a sort of prototype for a class. Cpp file contain the implementation of the class. I don’t know what type of file the actual code file is, in the case of the Arduino it’s an ino file.

I’m asking because when I create a large project for the Arduino I like to separate off all the house keeping functions into a few separate ino files so that I can keep the meat of the code clean.

Please could someone tell me ho I can do this for the spark, can I just create a .h file and put them in there? I want any global variables I create in the main source file to be accessible to the additional fills, in effect I just want to create multiple ino files like I can in the Arduino IDE, I’d also like to know how .h, .cpp and .ino files differ from one another.

Thanks for reading, RIck.

Hi @rickpbush,

Good question! Someone please feel free to jump in if I miss something!

When you create a new file in the build IDE it should create a .cpp (source file), and a .h (header file) for you, but you don’t have to keep them both. For each new header file, the build IDE will automatically add an #include "my_new_header.h" line at the top of your .ino file (main code file). .ino and .cpp files are essentially the same, but the .ino files get some extra processing so you don’t need to write stuff like function declarations.

tl;dr: You can organize your code however you like, but in general for the IDE you get one ‘.ino’ file, and as many .h / .cpp files as you like. Make sure you manage your includes, and if you’re missing the Arduino stuff in a .cpp file, just throw a #include "application.h" at the top.

Hope that helps!
Thanks,
David

Hi @rickpbush

The other thing you need to know is that like Arduino, for the main Spark .ino file, there is something called the preprocessor that takes care of what would normally go in the .h file, so you don’t need a .h file for your main program with loop() and setup().

For the other files you add which are generally .h and .cpp for C++ files, the preprocessor is not run. You need the .h files for what are called “forward references”. That’s where you have one function that calls another but the appear in the source code not in that order (or function A calls B and B also calls A). In order for the compiler to do its job, you have to tell it how the functions are called before they are used.

The preprocessor can fix this for the easy stuff in your .ino file, but there are cases it can’t handle in the C++ .h and .cpp files.

1 Like

Hi, thanks for the replies. Ok, so in the Arduino IDE, say I create a global variable in my main ino file, say int myVar = 1.

If I now create a new TAB in the IDE, and write a function (not in a class) that references myVar, it works, myVar is in scope. I’m struggling to find a way to do this in the spark IDE, if I create a new file (.h & .cpp) and write a function that tries to use a global var in my main file then the new cpp cant see it.

Hi @rickpbush

In C/C++ development outside of Arduino, what you are describing would not work without a special declaration called extern which lets the compiler know that you want to access something not normal visible.

On Spark, I have externed “other way” from a private thing in a C/C++ file to my main .ino file, but it should work both directions. I started to write an example for you, but I found this on stackoverflow.com:

1 Like

Hi, after having a think I think that I’m just being lazy. I’m writing sketches script style when I guess I should be designing a collection of cooperating classes. The problem I’m having is that I get an idea and hack out a simple sketch that works, then when I come to expand on it I find my source file is getting painfully long and difficult to work with. The obviouse solution is to besign a class model which would solve the problem. Thanks for your help with extern, I will research up on it but for now I’ve just got to bite the bullet and get the old grey matter figuring out what classes I need to create and use my noodle :slight_smile:

1 Like