FuelGauge multiple definition error when linking

I am abstracting some functions that vary from project to project. I sometimes use the on-board FuelGauge to read battery voltage and sometimes read battery voltage through other means. If I create a local library with function getBattV() that uses a FuelGauge object, the linking fails with multiple definition of '<FuelGauge Object Name>'.

My project looks like this:

├── lib
│   ├── boardv02
│   │   └── src
│   │       ├── boardv02.cpp  # uses the FuelGauge object
│   │       └── boardv02.h  # declares the FuelGauge object
├── project.properties
└── src
    └── project.cpp  # includes boardv02.h

I ran into another problem related to FuelGauge that makes me think it is not very robust. Is there anything obviously wrong with the above approach?

FuelGauge is an expression used by the device OS so you cannot just reuse it for something else.

I don’t think I’m using it for “something else” here but maybe you can explain where I’m going wrong.

In my library header I declare a FuelGauge object:

FuelGauge fuel;

In by library I use the object:

double getBattV() {
  return fuel.getVCell();
}

And in my main project file I call my library:

battV = getBattV();

The error produced is:

multiple definition of 'fuel'

That usually comes with some more messages that point you to the location of the conflicting definitions.

Another common error might be that you have your header files not guarded against multi-inclusion.

Thanks for the ideas @ScruffR. It turns out we can chalk this up to sloppy coding. The header file was guarded against multiple inclusion but I shouldn’t have declared the object there as that cause issues like this. Moving the declaration to the implementation file eliminated the error.

I don’t like having to use a global in the first place but that stems from the other issue I linked above.