What is good practice when source files need to use the same object

  8-bit, c++, embedded

I am mostly working alone on my projects and I am not always sure in what ‘others’ consider to be good or pad practices.

Image you have these source files and headers

foo.cpp
foo.h
bar.cpp
foo.h

Lets say that I need functions in both foo and bar to print text on a 16×2 LCD using Arduino’s LiquidCrystal.h library. I need to include the library and construct an object.

I can make 2 new files:
lcd.cpp and
lcd.h.
And in lcd.cpp I can make an lcd object.

#include <LiquidCrystal.h>
#include "lcd.h"

LiquidCrystal lcd(A5, A4, A0, A2, A1, A3) ;

Both foo.cpp and bar.cpp should include lcd.h

For as far I know I can do these two things.
I can either declare the lcd object with extern in lcd.h. Than every file which includes lcd.h has access to the global lcd object. So both in foo.cpp and bar.cpp I get to type

lcd.setCursor( 3, 1 ) ;

I believe this is very similar if not the same as Arduino does with the hardware Serial and Wire objects. You get to type

Serial.println(F("Hello world")) ;

In every source files which includes <Arduino.h>

Or I can make wrapper functions in lcd.cpp like:

void clear()
{
    lcd.clear();
}

void setCur(byte x, byte y)
{
    lcd.setCursor(x,y);
}

Using the wrapper functions does give me the infrastructure to implement easy outline and position functions like

void printAt(byte x, byte y, String text)
{
    lcd.setCursor(x,y);
    lcd.print(text);
} 

Are there different options for this scenario which are "better" for a reason?
Is one of the two methods considered "better" than the other? Or does it not really matter and is this matter highly subjective?

Source: Windows Questions C++

LEAVE A COMMENT