how to include a file with arrays

Hey there, ive got some huge more dimensional arrays, which I want to include into the code at the beginning, i wrote them into an empty sketch and saved them under arrays_hpp.pde

#include <arrays_hpp/arrays_hpp.pde>

boolean Play = false; // false = PAUSE true = PLAY
boolean PlayMode = true; // false = backwards true = forward
...

I cannot use the included arrays in this code.

How can i manage to include them so i dont have to scroll down to pages to the actual code every time because i ve got big array defnitions at the beginning of my code?

You could refresh a bit on the C/C++ first.

.pde is a file type designed for Arduino, not C or C++. Therefor you can't include it. All you can include is .h or .hpp.

Just to be on the safe side? How big is the array? You do know that the memory on the Arduino is not limitless, right?

You could refresh a bit on the C/C++ first.

All you can include is .h or .hpp.

Well, someone certainly needs a refresher on the C preprocessor.

Can you include .pde?

Is it recommended including a .c file?

So, for someone that doesn't quite know the #include directive, going further in depth on this is not even advisable. But please, enlighten us all on this matter. :slight_smile:

You can include any file extension you like, as long as it contains valid preprocessor directives or valid C/C++.

Ok... that doesn't make it right. I actually don't like C all that much, I'll start naming my files .z. It sounds more exotic. :stuck_out_tongue:

On another note, I find it funny that someone with a vast knowledge in the C/C++ preprocessor preferred to be a smart ass instead of replying to the original post.

@Flub, have you tried using " instead of <> on the include? Unless you create that folder and file in the Library path, you're not gonna get anything from it.

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:

  1. The main sketch has the setup() and loop() functions and anything else closely related to the main program.

  2. For external variables and/or functions, templates, classes, etc., I create a header file that has extern declarations, function prototypes, class definitions, etc.

  3. 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.

2 Likes