-1

It seems that every Arduino library uses the Arduino Serial library for printing debug messages. I'm currently trying to integrate this library into my project https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/I2Cdev. I am using my own python program to receive messages from my atmega 2560. I want to try out some libraries without changing their source code but because of the Serial library I would have to convert their Serial.print() calls to my own API calls. Since I would have to do that for every library I mess with I decided to create a wrapper for the Serial library in which I have a global variable named Serial. That Serial variable would then be my Serial wrapper defined as an extern.

I have the SerialWrapper in the serial namespace. I want a global variable Serial to be defined as extern, also in the serial namespace.

// SerialWrapper.h
namespace serial {

    enum SerialType {
        HEX,
        DEC
    };

    class SerialWrapper {
    public:
        SerialWrapper(ground::Ground& ground);
        void print(const char *string);
        void print(uint8_t data, SerialType type);
    private:
        ground::Ground& ground_;
    };

    extern SerialWrapper Serial;
}

Then in the source file I define the methods and global variable like so

// SerialWrapper.cpp
#include "SerialWrapper.h"

serial::SerialWrapper Serial = serial::SerialWrapper(ground::Ground::reference());

serial::SerialWrapper::SerialWrapper(ground::Ground& ground) : ground_( ground )
{

}

void serial::SerialWrapper::print(const char *string)
{
    ground_.sendString(string);
}

void serial::SerialWrapper::print(uint8_t data, serial::SerialType type)
{

}

Trying to test out the library, I call this in my main method

// main.cpp
using namespace serial;

int main( void )
{
    Serial.print("Hello World!");
}

To make the libraries compatible with my SerialWrapper, I need it to work this way. However, when I compile I get the error from main.cpp that there is an undefined reference to 'serial::Serial'

You can see all my source code under the mpu branch (soon to be merged to master) here https://github.com/jkleve/quaddrone

minnymauer
  • 374
  • 1
  • 4
  • 17
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user7860670 Jul 15 '17 at 05:55
  • @rangeme _'Every AVR library uses the Arduino Serial library for printing debug messages.'_ Where have you taken it from? It is **NOT THE TRUTH** – 0___________ Jul 15 '17 at 15:33
  • @PeterJ sorry, changed to say Arduino library. – minnymauer Jul 15 '17 at 15:44

1 Answers1

-1

To get the undefined reference to go away I had to move the declaration outside of the namespace like so

// SerialWrapper.h
namespace serial {

    enum SerialType {
        HEX,
        DEC
    };

    class SerialWrapper {
    public:
        SerialWrapper(ground::Ground& ground);
        void print(const char *string);
        void print(uint8_t data, SerialType type);
    private:
        ground::Ground& ground_;
    };
}

extern serial::SerialWrapper Serial;

I do not know why this has to be done. Maybe someone else can comment on the issue with having the extern declared in the namespace.

I also found this reference https://www.ics.com/designpatterns/book/namespace-example.html

Following the way found in the reference you can also setup the declaration and definition as follows

// SerialWrapper.h
namespace serial {

    enum SerialType {
        HEX,
        DEC
    };

    class SerialWrapper {
    public:
        SerialWrapper(ground::Ground& ground);
        void print(const char *string);
        void print(uint8_t data, SerialType type);
    private:
        ground::Ground& ground_;
    };
    extern SerialWrapper Serial;
}

The definition has to be changed to this.

// SerialWrapper.cpp
#include "SerialWrapper.h"

serial::SerialWrapper serial::Serial = serial::SerialWrapper(ground::Ground::reference());
minnymauer
  • 374
  • 1
  • 4
  • 17