Problem linking .h files

I am struggling to link files for some reason.

In the root of my project I have my .ino file and a directory called 'sensors'. The structure looks like this:

  • sensors
  • Wifi.h
  • Wifi.cpp
  • CI.ino

Wifi.h

#ifndef WIFI_H
#define WIFI_H

#include "Arduino.h"

class Wifi{
    public:
        Wifi();
        bool Initialize();
};


#endif

Wifi.cpp

#include "AZ3166WiFi.h"
#include "OledDisplay.h"
#include "Wifi.h"
#include "Arduino.h"

static bool _hasWifi = false;

Wifi::Wifi(){

}

bool Wifi::Initialize()
{
  Screen.print(2, "Connecting...");
  
  if (WiFi.begin() == WL_CONNECTED)
  {
    IPAddress ip = WiFi.localIP();
    Screen.print(1, ip.get_address());
    _hasWifi = true;
    Screen.print(2, "Running... \r\n");
  }
  else
  {
    _hasWifi = false;
    Screen.print(1, "No Wi-Fi\r\n ");
  }
  return _hasWifi;
}

CI.ino

#include "OledDisplay.h"
#include "AudioClassV2.h"
#include "AZ3166WiFi.h"
#include "AzureIotHub.h"
#include "DevKitMQTTClient.h"

#include "sensors\Wifi.h"

Wifi wifi;

void setup()
{
   ...
  hasWifi = wifi.Initialize();
  if (!hasWifi)
  {
    return;
  }
}

As written I will get 2 exceptions from the corresponding line in my ino file.

  • undefined reference to Wifi::Wifi()
  • undefined reference to Wifi::Initialize()

If I change #include "sensors\Wifi.h" to #include "sensors\Wifi.cpp" the code compiles, although I haven't tested it yet.

My code basically follows this tutorial verbatim so I am not sure why it won't compile.

Apologies if this is rudimentary. I haven't coded C++ since college so I am not going to be surprised if this is something silly I am overlooking.

Thanks!

That tutorial is about making a library:

First, make a Morse directory inside of the libraries sub-directory of your sketchbook directory. Copy or move the Morse.h and Morse.cpp files into that directory. Now launch the Arduino environment. If you open the Sketch > Import Library menu, you should see Morse inside. The library will be compiled with sketches that use it. If the library doesn't seem to build, make sure that the files really end in .cpp and .h (with no extra .pde or .txt extension, for example).

If you move your files to a libraries/Wifi directory they should work.

If you put the Wifi.cpp file in the same directory as your sketch it should become part of the build. There are other places that you can put it but I don't know where else works.

Thanks, John.

I tried adding a 'libraries' folder into my project directory but that didn't seem to have any impact. You are correct though, this is simply a problem with how the code is being built and not actually a problem with the code itself.

For right now I have just flattened my project structure so that I can get to my next round of testing. I will focus on the local builds and include paths a little later.

Thank you so much!

dcparsons:
I tried adding a 'libraries' folder into my project directory but that didn't seem to have any impact.

They meant the 'libraries' directory in the sketchbook folder.
If you want a 'libraries' directory for your sketch you have to put in a directory named 'src'. Every source file in the 'src' directory, or any subdirectory of the 'src' directory, is included in your sketch. They don't show up as tabs in the IDE so they are not as easy to edit. You might want to keep them in the main directory until you are satisfied that they are correct and then move them to the 'src' directory.
See: Redirecting