2

This is my code structure:

Flashtraining.h

#ifndef Flashtraining_h
#define Flashtraining_h

#include "Enhanced_SPIFlash_Marzogh\\SPIFlash.h"

class Flashtraining
{ 
  static SPIFlash myflash; //Should it be declared outside private/public? Does it matter?
  public:
    bool start_new_training();
    bool stop_training();
    /*Some more methods*/
  private:
    bool _Check_or_Initialize();                    
};

Flashtraining.cpp

#include "Flashtraining.h"
SPIFlash Flashtraining::myflash;

bool Flashtraining::start_new_training(){
/*Do smth. with myflash object*/
}

bool Flashtraining::stop_training()
{
  /*Also do smth. with myflash object*/
}

main.cpp

#include "Flashtraining.h"

Flashtraining training; //Create object of class Flashtraining

training.start_new_training(); //Ignoring the main funcion, return values etc.

SPIFlash has no constructor and only non-static members. If you need this file too, comment and I will provide it, but I am sure it is not the problem here.

My problem: When executing this code, I get

C:\Users\--\Flashtraining.cpp.o: In function `Flashtraining::_Check_or_Initialize()':

C:\Users\--\Flashtraining.cpp:265: undefined reference to `SPIFlash::begin(unsigned char, unsigned long)'

C:\Users\--\Flashtraining.cpp:268: undefined reference to `SPIFlash::getJEDECID()'

Note: _Check_or_Initialize() is one of the functions declared in the Flashtraining.cpp file. It is declared just like the other ones I have provided.

The functions in SPIFlash are declared non-static and implemented correctly.

What I don't understand: Because I use the static keyword, I can just declare an object(myflash of type SPIFlash) in the header file (Flashtraining.h) without initializing it, right? This also prevents redefinitions. Because of the One definition rule in c++, I initialize the variable once in Flashraining.cpp, where it is used. I then should be able to use (.) notation to access non-static members (the functions, like myflash.start_training()), or not?

Why is this giving me an undefined reference and how can I fix it?

I looked at this post and think that I changed my code accordingly, but it doesn't work.

Additional information: This is .ino code, which is based on c++. Everything written in c++ works normally. Using -std=gnu++11.

Community
  • 1
  • 1
jubueche
  • 763
  • 5
  • 24
  • 1
    Guys, am I not correctly initializing myflash here: SPIFlash Flashtraining::myflash; in Flastraining.cpp? – jubueche Jun 23 '18 at 17:07
  • so is your question *"static SPIFlash myflash; - Should it be declared outside private/public? Does it matter?"* or your undefined reference? – Joseph D. Jun 23 '18 at 17:14
  • I want to resolve the undefined reference. This is my priority, but if you know the answer to the other one, it would be nice to hear it. – jubueche Jun 23 '18 at 17:16
  • 1
    Just confirming, have you used this library? or have you just pasted the code in the same folder as your ino file? – Nitro Jun 23 '18 at 17:39
  • The library is inside a folder in the same directory as my .ino file. (Talking about SPIFlash). I am including it using #include "Enhanced_SPIFlash_Marzogh\\SPIFlash.h" It doesn't give me any compilation errors, so I thought it's fine. – jubueche Jun 23 '18 at 17:43
  • OK wow it works now. Thank you so much @Nitro! – jubueche Jun 23 '18 at 17:45
  • I have no clue for what, but you are welcome. @jbuchel – Nitro Jun 23 '18 at 17:48

1 Answers1

1

Go to Arduino IDE preferences, and enable verbose checkbox for compilation.

Compile the code again. I think you will find the problem here itself, if not, look through the output for a file which has the extension .ino.cpp.

You will get the absolute location of the file in the output. Use the absolute path and open the file in a text editor. Once in confirm that the function

SPIFlash::begin(unsigned char, unsigned long)

is declared (If it is, I am at a loss). If not, then search through your code for the same declaration.

Your declaration will probably be surrounded by some #ifdef, #endif preprocessor, which is keeping your code undefined. Figure out if you want the preprocessor or not and edit it accordingly.

Edit: Arduino IDE only compiles cpp files present next to the ino file, or in the libraries, whose headers have been used. So if your ino.cpp file does not have the function declarations, it might be because the cpp files are in some other folder or in a subfolder next to the ino file.

Nitro
  • 1,063
  • 1
  • 7
  • 17
  • I think Arduino just had problems including all the files of a subdirectory. After moving the folder to the libraries folder in my Arduino folder, everything worked just fine. – jubueche Jun 23 '18 at 17:54
  • Yes this is repeatable. Seems like the Arduino does not parse for all the folders inside the ino file for cpp file. The header file worked for you, because you mentioned it with the folder name. But the cpp file never compiled, and gave you the undefined function error. – Nitro Jun 23 '18 at 18:13
  • @jbuchel: Please do 1 of two things: Either Mark my answer as correct (That's preferable to me), or post your own answer, and mark it correct. I am saying this, as I believe that it's not right to keep a thread open without an answer, especially if the query is solved. – Nitro Jun 24 '18 at 01:58