I'm new to the Arduino but an old hand with electronics and C programming. I'm trying to understand the sketchbook concept and how tabs work within a sketch. This all started when my code started getting bigger and I decided to break it up into modules.
So, first question, it appears that I have no control over the order or position of the tabs. The IDE appears to place them in order by name case sensitive alphabetically and I can't do anything about it, right?
Second, if I give the tabs generic names like "foo" and "bar" it seems during the compile phase to just concatenate them all together and as long as there aren't any backwards references things work fine. If I rename them to "foo.c" and "bar.c" then it seems to treat them as separate files and everything blows up. This means I have to create a .h file full of "extern ..." statements and include that in every module, right?
I can provide a suggestion for part of it - you can rename the tabs. I do this in mine:
a_presetup
b_setup
c_void loop
d_breakout_of_some_parts
e_breakout_of_other_parts
They all seem to get stored as x_name.pde, except in one case where I explicitly named a tab pitches.h for use with Tone.
Letting them have the default (.pde) extension (probably shows up as null) causes them to be treated like an arduino sketch; they get the arduino pre-processing (which includes concatenation and generation of function prototypes to prevent forward reference problems.) If you name them .c or .cpp, they'll be treated as "real" c or C++ files, and WON'T get the special sketch pre-processing.
... old hand with electronics and C programming. I'm trying to understand the sketchbook concept and how tabs work within a sketch....
One way to do things, and, I'm thinking, a way with which old hands at C programming should be comfortable:
Put a header file in a tab named "something.h" If functions in other tabs (main sketch or other .h tabs or .cpp tabs) need something from that header (extern variable declarations or function prototypes or C++ template definitions or whatever...) then the code in the other tabs will #include "something.h" You can have more than one header tab. I like to keep a separate tab for each header rather than lumping them all together. Of course, any given .h file can contain information about lots of (related) functions.
Put executable code (function implementations) and extern variable definitions in .cpp files (in .cpp tabs). If the code in a given .cpp file is related to "something.h," I name the tab "something.cpp"
Here's how it goes when Arduino compiles your sketch:
Arduino embeds your main sketch file into a .cpp file that, among other things, includes an Arduino core header file that defines everything that the compiler needs to know about Arduino stuff. This includes Arduino-specific typedefs, Arduino core library function prototypes, etc.
Arduino compiles that into an object file.
Arduino compiles the code in your .cpp files (in .cpp tabs) into their own individual object files
If your sketch needs a library file, it will #include the header file(s) for that library. Every library header and implementation file must be in a place that Arduino can find them (in the libraries subdirectory in the Arduino distribution or under your sketchbook/libraries directory.) Each .c and/or .cpp file in these libraries will be compiled into its own object file.
Arduino creates a core library (archive) file containing all of the core library object files.
Finally, Arduino creates an executable by linking everything together.
All of this stuff happens in a temporary directory whose location depends on the operating system you are using. Contents of the temporary directory are refreshed each and every time you compile your sketch and they (probably) go away when you exit the Arduino IDE.
To see the sequence of commands (and to find the location of the temporary files on your system), open a sketch in the Arduino IDE, hold down a Shift key and click the "Verify" icon.