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