We will create two tabbed windows in addition to the main sketch window. One for a .h file and one for a .cpp file
1. Create a brand new sketch. You will have an edit window named "sketch_may01a" or some such thing.
2. Select File->Save As from the menu bar and name the saved sketch TestFileStuff.pde
3. Create a new tab (Click the arrow at the right-hand end of the tab bar.) Name the new tab fileStuff.h
4. Paste the following into the fileStuff.h window:
// fileStuff.h
//
// Don't really need inclusion guards here, but it's not
// a bad habit to get into
//
// Declare external variables and give function prototypes here
// for stuff to be included from your main sketch (or from
// anywhere else in your project
//
// davekw7x
//
#ifndef FILESTUFF_H__
#define FILESTUFF_H_
extern int x[];
extern char aString[];
const int xSize = 20;
#endif
5. Create another new tab. Call it fileStuff.cpp
6. Paste the following into the fileStuff.cpp tab:
//
// fileStuff.cpp
//
// This contains variable definitions for things declared in
// fileStuff.h
//
// If there were functions declared in fileStuff.h, I
// would put their implementations here.
//
// davekw7x
//
#include "fileStuff.h"
// Since xSize = 20 in fileStuff.h, memory for twenty ints is allocated.
// I the initializer list has nine elements, so all after x[8] are initialized to zero
int x[xSize] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// This array is sized automatically by the compiler. The array size is strlen(aString) + 1
char aString[] = "Hello from fileStuff";
Now, paste the following into your main sketch edit window:
//
// TestFileStuff.pde
//
// Show how to get at variables in other non-library files in
// this sketch.
//
#include "fileStuff.h"
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("x[4] = ");
Serial.println(x[4]);
Serial.print("Number of elements in x = "); Serial.println(xSize);
Serial.print("aString = ");
Serial.println(aString);
delay(1000);
}
If you save the sketch now, you will see three files in the TestFileStuff sketchbook directory:
-
TestFileStuff.pde
-
fileStuff.h
-
fileStuff.cpp
You can add other .h and .cpp files by creating more tabs. All will be saved in this sketch's directory. For every .h file that you include in your main sketch, the Arduino IDE will collect the files into its build directory and will compile the .pde file into its own object file and will compile each .cpp file into its own object file. Then it will link the sketch object files with the core Arduino library files (and object files that were created for any other library that you have imported into your sketch). Current version of Arduino re-compiles everything each time; there is no danger of "leftovers" from previous compilations.
Output from this little test:
x[4] = 5
Number of elements in x = 20
aString = Hello from fileStuff
There are about a million other ways to do stuff like this (maybe more) but I (usually) choose to do things in Arduino in ways that closely resemble the way that I organize "regular" C and C++ projects.
Recap:
For simple projects I just have one file that has everything. For more interesting ones:
-
The main sketch has the setup() and loop() functions and anything else closely related to the main program.
-
For external variables and/or functions, templates, classes, etc., I create a header file that has extern declarations, function prototypes, class definitions, etc.
-
Associated with that header file, I make a .cpp file with the same base name. That's where I put the variable definitions, function and class method implementations, etc. (Note that if the header file only has a template (or templates) there may not be an associated .cpp file. That's OK)
If there are several classes or groups of related functions (but not closely related to other stuff in the project) I create a .h file and a .cpp file for each such group.
Bottom line recommendation: Include header files. Let the linker take care of the .cpp files for you.
That's just an opinion. My opinion. It's worth exactly as much as you want it to be worth.
Take or leave it. No hard feelings.
Regards,
Dave
Footnote:
You don't have to go through the "create tab" stuff inside the Arduino IDE. You can create your own .h and .cpp files with your favorite text editor (probably vim, right?) and save them into the sketch directory. The next time you restart Arduino and open that sketch, you will see tabs for the .h and .cpp files.