Dear community,
I have a namespace
related issue, which I am unable to solve, even with Dr. Google, as I am not familiar with namespaces at all.
With my latest project, I would like to use the following two libraries (installed latest version via library manager) in a sketch running on an Arduino Nano:
- INA219_WE (voltage and current sensor readout)
- Low-Power (save some power in between sensor readouts)
Both libraries work well independently.
However, if I import both libraries in the same sketch, I get a redeclaration error:
Sketch
#include <INA219_WE.h>
#include <LowPower.h>
void setup() {
}
void loop() {
}
Error
In file included from <...>\libraryConflictExample\libraryConflictExample.ino:2:0:
<...>\Arduino\libraries\Low-Power/LowPower.h:29:2: error: redeclaration of 'ADC_OFF'
ADC_OFF,
^~~~~~~
In file included from <...>\libraryConflictExample\libraryConflictExample.ino:1:0:
<...>\Arduino\libraries\INA219_WE\src/INA219_WE.h:59:5: note: previous declaration 'INA219_MEASURE_MODE ADC_OFF'
ADC_OFF = 0b00000100,
^~~~~~~
As the error message indicates, both libraries use the constant ADC_OFF
. A similar problem was discussed for different libraries. There, the author fixed the problem by modifying one of the libraries. For compatibility reasons I would like to avoid changing the (external) library code.
In the aforementioned thread @PieterP suggested to "wrap all library code in a namespace".
So what I tried is this:
#include <INA219_WE.h>
namespace LoPo {
#include <LowPower.h>
}
void setup() {
}
void loop() {
LoPo::LowPower.idle(LoPo::SLEEP_8S, LoPo::ADC_OFF, LoPo::TIMER2_OFF, LoPo::TIMER1_OFF, LoPo::TIMER0_OFF,
LoPo::SPI_OFF, LoPo::USART0_OFF, LoPo::TWI_OFF);
}
For the library import this works fine, but the function call raises an error:
<...>\AppData\Local\Temp\ccbloPUo.ltrans0.ltrans.o: In function `loop':
<...>\Arduino\libraryConflictExample/libraryConflictExample.ino:10: undefined reference to `LoPo::LowPower'
<...>\Arduino\libraryConflictExample/libraryConflictExample.ino:10: undefined reference to `LoPo::LowPower'
<...>\Arduino\libraryConflictExample/libraryConflictExample.ino:10: undefined reference to `LoPo::LowPowerClass::idle(LoPo::period_t, LoPo::adc_t, LoPo::timer2_t, LoPo::timer1_t, LoPo::timer0_t, LoPo::spi_t, LoPo::usart0_t, LoPo::twi_t)'
collect2.exe: error: ld returned 1 exit status
Hence, I was wondering: Is it possible to wrap the entire contents of a library including function calls in a namespace without touching the external library code? And if yes, how would I do this?
Any help would be appreciated,
Max