LCD I2C libraries incompatibility

Two "important" LCD libraries seem to cause compatibility issues.
I have a program that works well with this library by malpartida, but as soon as I install this library by F. DE Brabandere, my original program does not compile anymore: library error.
Reason: these libraries use identical files for LiquidCrystal_I2C.h and .cpp

Academic question: why do developers not pay attention for this kind of issue before publishing their work?

Practical question: what to do if I would like to have both libraries available? Or is this besides the question and should the need for more than one library for the same hardware be solved by "proper" programming?

Because most of them aren't developers, they're hobbyists like you.

If you are looking for a good library for I2C enabled LCDs with the hd44780 controller (1602, 2004) the hd44780 library by Bill Perry (@bperrybap) is the best. It is faster, has better features, is currently maintained and the author is often on the forum to answer questions. The library is available via the IDE library manager.

And it avoids the confusion when you post code as to which library to use.

2 Likes

IMO, the ones at fault here that created issues like this are actually the Aduino.cc developers.
They are the ones that, IMO, made some unfortunate design decisions that has created the issues related to library header file name collisions.
Years ago, I had offered advice on some ways to solve this when moving to the 1.5x IDE version and the 1.5x library format but they insisted on a few things that meant the issue can never ever be fully resolved.
If things had been done slightly differently there would never been an issue with collisions since the sketch would be able to fully specify library it wanted by using a better header file name that specifies the library/directly in the header file.
i.e.
#include <newLiquidCrystal/LiquidCrystal_I2C.h>
or
#include <LiquidCrystal_I2C/LiquidCrystal_I2C.h>

It is a very small trivial change.
While the change can be made at any time, the best time to have made it was when when the library format was changing as well, but they didn't want to do it.
Even sadder is if they would make the change the way I recommend, it would not affect any existing sketch or library code.
Sketches could explicitly specify the header using the directory name. IF they didn't specify the leading directory name, the IDE would pick it for them using the "guessing method" just like it does today.

Over the years, there has been quite a bit of work to patch things to try to help the issue, which has made things better, but again, due to some things they have decided to do, it is impossible to ever fully resolve.
There are some additional library issues that are going crop with IDE 2.0.0 given some of the changes / differences on how libraries are handled that IDE.

Currently there is a priority on how the library to use is selected when there are name collisions.
What is creating the issue in this case is that newLIquidCrystal and LIquidCRystal_I2C both have a header file named LIquidCrystal_I2C.h
If a sketch includes <LiquidCrystal_I2C.h> the IDE will give priority to a library with the same basename as the header file. i.e. LiquidCrystal_I2C

While I would recommend using the hd44780 instead,
there is a way to game or trick the Arduinoi.cc IDE library system to work with both newLIquidCrystal and LiquidCrystal_I2C installed and yet get it to select the one the sketch wants to use.
A sketch can use this method to select either one, but it will not be able to use both at the same time. Normally that is not an issue.

If you want to force the use of newLiquidCrystal LiquidCrystal_I2C you need to include a header file that is unique to newLIquidCrystal, prior to the i/o class header file that has the name collision.
By doing that the IDE will determine the library to use based on that unique header rather than the header file that has the collision which would cause the IDE to select the other library since its directory name matches the file basename which gets priority in the the "guesing game".

For newLiquidCrystal,
name collisions can be for LiquidCrystal_I2C.h or LiquidCrystal.h
i.e.

In the case of wanting to use the newLiquidCrystal library LIquidCrystal_I2C i/o class instead of the LiquidCrystal_I2C library,
simply do this to force the use of newLiquidCrystal

#include <LCD.h> // will force using NewLiquidCrystal library
#include <LiquidCrystal_I2C.h>

--- bill

1 Like

Hi Bill, what an good explanation!!

What do you mean by "basename" (in relation to IDE prioritizing)?

And indeed, this is a perfect solution: including the

```
#include <LCD.h> // will force using NewLiquidCrystal library
```

before the actual LCD library call allows compilation even with the competing library by F. De Brabandere installed. Big thank you!

Just to be clear: not including the above code will prioritize the F. De Brabandere library: but why? I guess I had not really understood that part of your explanation?

I think you missed the part about how the IDE guesses and finally selects the library when there is a header file name collision. The IDE prioritizes the library that is in a directory name that matches the basename of the header file.

for a headerfile named foo.h the basename is foo

--- bill

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.