Use of Libraries from functions located in .h/.cpp file pairs.

I have a question concerning the use of "#included" libraries from functions setup and existing in .h/.cpp file pairs.

I am running a Mega board with an ADA SD breakout board, an ADA GPS breakout board and a U8g2 library for two. GLCD display.

The program I am working with is getting quite large so I am starting to move many of the tested and mature functions to ,h/.cpp file pairs to get them out of the main .ino file.

In doing this I have run into a bit if a snag. It appears that at least some of the libraries won't run from calls initiated from outside of the file (.ino, .cpp) where the libraries were initiated. Too be fair, I have not at this time tested all of the libraries I am using.

I have contacted the source for one of the libraries I am using and ask them about the issue and was told that the library would not prevent this and that I should contact the people responsible for the IDE I was using.

I am using the Arduino IDE..

Is this an issue??

Is there a work around?? or

Is this a fact of life and I will just have to deal with the limitation?

At this time I am basically just starting out doing this but I think I have the basics working (at least some of the time). I'll try to give a paraphrased code example.

This will work, the SD startup located in .ino file as are all related functions.

//  Ini file

#include <Arduino.h>
#include <SD.h>

String Infile = "TestFile.txt;

void EraseFile(String Infile) {
// Delete the file:
      Serial.println(Infile);
      if (SD.exists(Infile))  SD.remove(Infile);
      else Serial.println("File Not Found");
}

void setup() {
                    Serial.begin(74880);
                   SD.begin(53);
                 }

void loop() { EraseFile(String Infile); }

This will work, the SD startup located in .cpp file as are all related functions. Functions used to call the SD library's functions.

//  Ini file
#include <Arduino.h>
#include "SD_Functions.h"

void setup() {
      Serial.begin(74880);
      SetupSD();
}

void loop() {
                Deletefile();  
                Serial.println("DONE");
}


// SD_Functions.h
#include <Arduino.h>
#include <SD.h>
#include "SD_Functions.h"

      void SetupSD();

      void Deletefile();  

      void EraseFile(String Infile);

// "SD_Functions.cpp"
#include <Arduino.h>
#include <SD.h>
#include "SD_Functions.h"

String Infile = "TestFile.txt";

 void SetupSD() { SDBegin(53); }

 void Deletefile() { EraseFile(Infile) };  

 void EraseFile(String Infile) {
                                       Serial.println(Infile);
                                       if (SD.exists(Infile))  SD.remove(Infile);
                                       else Serial.println("File Not Found");
                                      }

This will not work!! The SD startup is done in the .ino file. The erase function is located in the .cpp file.

//  Ini file
#include <Arduino.h>
#include <SD.h>
#include "SD_Functions.h"

void setup() {
                    Serial.begin(74880);
                    SD.begin(53);
                 }

void loop() { EraseFile(Infile); }

// SD_Functions.h
#include <Arduino.h>
#include <SD.h>

  void EraseFile(String Infile);

// "SD_Functions.cpp"
#include <Arduino.h>
#include <SD.h>
#include "SD_Functions.h"

String Infile = "TestFile.txt;

 void EraseFile(String Infile) {
                                       Serial.println(Infile);
                                       if (SD.exists(Infile))  SD.remove(Infile);
                                       else Serial.println("File Not Found");
                                      }

The above three (3) examples are a simple example of the concept of which I am asking about. The code should compile but no guarantees. I am trying to present a concept here not necessarily functional code.

You might ask why I would want to do this but it has to do mostly with the amount of code I have generated to get done what I am trying to get done.

The SD code alone is about 600 lines. The code I have generated to the GPS work is probably closer to 1000 lines of code. The graphics code will certainly exceed 1000 lines of code before I am done and the code I use to collect and display the engine data from the engine's ECU probably runs another 500 lines of code.

This thing is becoming difficult to work on and the compile and linking times are becoming ridicules.

I hope this helps better explain where I am trying to go.

Any in site you can provide will be greatly appreciated. If I have to startup and run the hardware library from the file I use it, so be it. I will just have to work with that limitation.

I don't see that but either way, the variable is not required outside of the function from which it is used.

If you declare more than once, the compiler/linker will complain. The concept is the point of the post not necessarily the code itself.

Thanks for your input

At this point, I have become a bit confused with the definitions issue.

Example code #3
Point 1

In my sending the variable "Infile" and giving it to the function "Erasefile" as a Parameter(Parm). I should have defined it in the .ino file. I understand..

Point 2
A), In the .h file, It is my understanding that I am explaining the function "Erase file" for everybody outside of the .h/.cpp file pair world.

B), You are also saying that in the definition of the function "Erasefile" I am actually defining a new variable to be used by the function. And as such, the new variable needs to be defined in the .cpp file.

If this is true, I agree and understand that having the same name is asking for trouble.

It was my understanding that we were passing a variable not assigning values to new variables. Am I correct in my new understanding if what is going on??

I got it..

Boy you have the patience of Job.

I will from this point on give them different names.

Now back to the original problem. I will try to better define the issue.

If you #include a library in the ino. (ie SD)

Initialize it in the ino's setup loop. (ie SDbegin():wink:

And then try to call one of its functions from a .cpp, the compiler will come back to you and tell you
it can't find the function even though you #included the library in the .cpp. That was what I was trying to explain in the three (3) examples I gave.

If you #include the library in the ino and initialize it in the ino you can run it from the ino.

If you #include the library in the cpp and initialize it in the cpp you can run it from the cpp.

If you #include the library in the ino and initialize it in the ino and try to run it from the cpp. The code will not compile. The same will happen it you try it the other way around.

I am thinking that I might be able to #include the library in the .cpp, initialize the library in the .cpp. And then define all of the functions to be used in the library in the .cpp's .h file and it might work. But I don't think it would work the other way around.

I'm glad to here that it is my problem and not a compiler/linker issue.

I'll get back to you tomorrow. Its 9:00 PM and I haven't had dinner yet.

Thanks a lot for your patience