ttlappalainen / NMEA0183

Library for handling NMEA0183 messages
69 stars 44 forks source link

Duplicate definitions of the NMEA2000 object #33

Closed sailingfree closed 2 years ago

sailingfree commented 2 years ago

Hi Timo, I'm using this excellent library in an ESP32 project that I've split into many separate files for easier maintenance and reuse.

https://github.com/sailingfree/n2k-gateway

If I include the NMEA2000.h in more than one source file I get multiple instances of the NMEA2000 object. I see that the NMEA2000_CAN.h selects the appropriate low level class, so its not just a case of moving the definition to the application. I've added a work-around using something like this:

#ifndef defined_NMEA2000
tNMEA2000 &NMEA2000=*(new tNMEA2000_esp32());
#define defined_NMEA2000
#else
extern tNMEA2000 &NMEA2000;
#endif

and defined that in my application files but thats clumsy.

The other thought was to #define the type in the NMEA2000_CAN.h for each board and have the application define the object. Something like

define NMEA2000_TYPE tNMEA2000_esp32

and in the application:

tNMEA2000 &NMEA2000=*(new NMEA2000_TYPE());

That would break a ton of applications of course.

Any thoughts?

ttlappalainen commented 2 years ago

You can include NMEA2000_CAN.h once and just add to other code extern tNMEA2000 &NMEA2000; NMEA2000_CAN.h is meant for easy selection for beginners. For projects with specific processors I have separate module for N2k and include required files. Then just use extern to use it outside.

You can just simply forget all thoughs braking down compatibility. I either do not see the point of changing current logic. I have on my project NMEA2000_Service.h containing

include

extern tNMEA2000 &NMEA2000; and then on NMEA2000_Service.cpp I create tNMEA2000 object and initialize it.

sailingfree commented 2 years ago

Thanks for your thoughts, both good points. Sorry, I wasn't seriously suggesting breaking things, I know from long experience that is just plain wrong and how much pain it causes.