Force Concatenation Order for Multiple File Sketches

I am trying to develop a multiple file sketch and am getting "not declared in this scope" errors because one of my files (one with some definitions) is being concatenated AFTER the main INO. I have tried renaming to change the order in the directory but this does not seem to help.

Help would be appreciated before I toss an Arduino in the Thames out of frustration!

Wisar

Post your code, otherwise we are guessing at the problem

I was hoping there was something obvious that I was missing…

Here is the primary INO (forms4UtftTest.ino):

#include <UTFT.h>
#include <UTouch.h>

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

UTFT myGLCD(ITDB32S, 38,39,40,41);
UTouch myTouch(6,5,4,3,2);

void setup()
{
    // For debugging messages
    Serial.begin(57600);

    myGLCD.InitLCD();
    myGLCD.clrScr();
    myTouch.InitTouch();
    myTouch.setPrecision(PREC_MEDIUM);
}

void loop() {
    while (true)
    {
        if (myTouch.dataAvailable())
        {
            myTouch.read();
            int x=myTouch.getX();
            int y=myTouch.getY();
            for (int i = 0; i < DEF_MENU; i++) {
                if (x >= Def_Menu[i].x && x <= Def_Menu[i].x1) {
                    Serial.println("Pressed " + Def_Menu[i].name);
                }
            }
        }
    }
}

Here is it’s companion that is getting sucked into the compiler behind the above( Forms4Utft.ino):

// Generated by Forms for TFT a companian to www.Form-Builder-for-PHP.com
//

// Enable storage of literals in program space rather than RAM
#include <avr/pgmspace.h>

struct Forms4Utft {
    String name;
    int x;
    int y;
    String formatName;
    int width;
    int height;
    String type;
    String color;
    String backColor;
    int round;
};

const int DEF_MENU = 12;

const Forms4Utft Def_Menu[DEF_MENU] = {
     {"selStats", 15, 195, "BigFontS", 90, 35, "btn", "000000", "      ", 0},
     {"selGraph", 215, 195, "BigFontS", 90, 35, "btn", "000000", "      ", 0},
     {"line2", 15, 185, "", 300, 2, "hline", "000000", "      ", 0},
     {"line1", 15, 95, "", 300, 2, "hline", "000000", "      ", 0},
     {"testStatus", 15, 105, "BigFontS", 280, 0, "txt", "000000", "      ", 0},
     {"header", 15, 5, "BigFontS", 280, 0, "txt", "000000", "      ", 0},
     {"maxSustainedBar", 15, 160, "", 290, 15, "hline", "000000", "      ", 0},
     {"selTest20", 115, 35, "7SegNumFontR", 90, 50, "btn", "000000", "      ", 1},
     {"selTest30", 215, 35, "7SegNumFontR", 90, 50, "btn", "000000", "      ", 1},
     {"maxSustained", 85, 130, "BigFontS", 200, 15, "txt", "000000", "      ", 0},
     {"selTest10", 15, 35, "7SegNumFontR", 90, 50, "btn", "000000", "      ", 1},
     {"obsSec", 15, 130, "BigFontS", 40, 0, "txt", "000000", "      ", 0},
};

And the exact error messages:
Forms4UtftTest.cpp: In function ‘void loop()’:
Forms4UtftTest.cpp:33:33: error: ‘DEF_MENU’ was not declared in this scope
Forms4UtftTest.cpp:34:26: error: ‘Def_Menu’ was not declared in this scope

You need a header file for sharing functions with the second file, which defines all of the functions prototypes, and include that at the top of your main sketch:
Main sketch

#include "myLib.h"

int* p_x;

void setup(){
   p_x=get_p_x();
}

void loop(){
  Serial.print(myFunc(*x,1));
}

myLib.h

#ifndef MY_LIB_H
#define MY_LIB_H

int* get_p_x(void);
int myFunc(int a,int b);

#endif

myLib.cpp

#include "myLib.h"

int x=0;

int* get_p_x(void){
   return &x;
}

int myFunc(int a,int b){
   return a+b;
}

To share variables you need to either make them global with extern (quicker to implement, but not good practice and likely to cause problems down the line) or use pointers and have accessor functions to pass the variable between the files (like I did in the example to share x between the two files).

TobyB:

Thanks for the help…but…are you sure that is the only way forward? I have working libraries but as I read the following from Hacking the Arduino I ‘should’ be able to concatenate files as I am trying to do (unless, of course, the concatenation is only from the back…):

Multi-file sketches

A sketch can contain multiple files (tabs). To manage them, click on the right-facing arrow just above the scroll bar near the top of the environment. Tabs have one of four extensions: no extension, .c, .cpp, or .h (if you provide any other extension, the period will be converted to an underscore). When your sketch is compiled, all tabs with no extension are concatenated together to form the “main sketch file”. Tabs with .c or .cpp extensions are compiled separately. To use tabs with a .h extension, you need to #include it (using “double quotes” not ).

I guess I could construct it as a “.h” … but as this is generated code I would rather have it be simpler than not simpler.

Wisar

You could make the second sketch file the main file, and the one with loop and setup the secondary one, rename your folder to the same name as the second file; but bother? If you're going to split your code across multiple files you might as well do it properly.

I have experimented with this (successfully I think) based on recent advice on this forum.

As far as I can see you can have multiple .ino files in a sketchbook folder. The file with the same name as the folder is loaded first. The other .ino files are loaded in alphabetical order.

There can be global variables in any or all of the .ino files but, for example, the globals in aaa.ino can be read by bbb.ino (because bbb is loaded after aaa) but aaa.ino can’t read the globals in bbb.ino.

Methods/functions seem to be callable across all of the files without regard for the order in which they are loaded.

I haven’t used any .h or .cpp files.

…R

Well. Darn. Nothing is ever easy.

...

Actually....I just included the file above, as generated, and it seems to be working. Or at least I am not getting the same errors and have others that I can now attack!

Thanks for the help,
Wisar

Dear anyone

Sorry for replying to an old topic.
I was looking for exactly the kind of example that "tobyb121" has posted.
Problem is, it wont compile as it stops with error :

'x' was not declared in this scope

If i declare the variable x in the myLib.h , then the error it stops with is :

invalid type argument of unary '*' (have 'int')

What am i missing? (Has this perhaps changed with the newer version of the IDE ?)

Thanks for anyone who can help.

Dorzo_:
What am i missing?

What we are missing is code for the program you are talking about. Post your program.

And please use the code button </>so your code looks like thisand is easy to copy to a text editor

…R