How can a library know what other libraries are in use?

Hi all!!!

So I wrote a small graphics library for an ESP32 project. Now for a new project, I want to reuse that library on an UNO. How can I get my library to know what platform its running on?

What I really need to know is what graphics library is being used.

Is there some elegant way to check what graphics library is in use, like an #ifdef to check for used libraries that I should add to my library?

Thanks for your input,
Randy

I'm not familiar but I've seen a choice in program like the next one.

#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  ;
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  ;
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  ;
#else
  ;
#endif

Maybe such variables depending on the choice of microcontroller will work for you.

you can use ifdef and __has_include

here I use it to detect networking types:

Most libraries start with an include guard like this:

//Include Guard- This prevents the code from being included into the program binary multiple times.
#ifndef MyBlink_H
#define MyBlink_H

So, if I need to know at compile time if MyBlink is already included somewhere else, I just need to test for the guard:
#ifdef MyBlink_H

Does your library depend on another library? if so, just include another library, this will force the user to download the required library if they don’t have it. Why do you need to know if specific library is included?

In a general sense, libraries CANNOT know what other libraries are in use, unless they all #include each other, which will create MANY problems. Libraries are separately compiled, so each knows only what IT requires to compile successfully. You would have to #include a .h file containing #defines or similar listing ALL libraries needed for them all to have such knowledge.

Bottom line: Give up. It's a fools errand. They should not NEED to know so much. If they do, then your system design is badly flawed, and you are constructing a giant can of worms.

Hey, thanks for all the replies!!!

I admit it was late last night when I posted my question and I realize now, I left out some of the details. So I will fill in the details now....

The library I wrote is a GUI library for graphical displays, like a typical 2.8 inch, 320x240 LCD display.
It's a simple library that creates graphic buttons, switches, and sliders. When I wrote this, I was using an ESP32 and the code used the TFT_eSPI library to create my graphic images. Now I want to use this GUI library on an UNO and the go-to graphics library there is the Adafruit-GFX library. Those 2 libraries have different functions and methods, so I need to know what library is being used.

Thanks flashko for your input, but I was wrong when I mentioned what platform I was using. I need to know what library is used.

I will look into this! When it comes to this, I guess I don't really know the order of the compiling process. If my main program has an #include is that code compiled before the rest of the program? I would guess yes. Then if the previous #include has another #include in it, they are all complied before the main program is, correct? Are all functions & methods from all the #include code then available to the main program?

Yes, my library needs to know what graphic library is being used so it knows what functions/methods to use to create the GUI.

Thank you! I think I have had 'MANY problems' before when writing the code on the ESP32. I think this is where my issue is, not understanding the order of compilation and when data/methods/functions from one library is available to the main code.

Anyway, I'm off to code some more on this!
Thanks,
Randy

You should consider "wrapping" the graphis library in a hardware-agnostic library that wraps the graphics library, so your application talks to the API of the wrapper, not to either graphics library, and the wrapper deals with the underlying graphics library, making them both look the same to the application. Ideally, the application should not know or care which graphics library is actually being used, so any code that NEEDS to know goes into the wrapper, and the application works with whichever wrapper it is compiled with.

@RayLivingston - I hope you see this post and respond.

All right, hmmm, I'm trying to figure this out:

But for some reason, I can't quite get it..... Or I'm getting confused somewhere... with this concept.

This part I get:

And I get this part too:

Here's what confuses me:

So I would have to have a wrapper for each graphics library used? And someone using my GUI library would have to #include the wrapper for whichever primitive graphics library they are using. If I have to write a wrapper for each primitive graphics library that is being used, then why not just write a different GUI library for each primitive graphics library? That is what I am not understanding.

I guess what I was looking for was a way to avoid for anyone using my library to have to configure my library to work for them. The Adafruit_GFX library pretty much works out of the box, just change a few setting for the hardware setup and done. The TFT_eSPI library requires a programmer to edit a setup header file defining the hardware being used.

Writing this gives me a bit of a clearer focus now.

Thank you RayLivingston

Randy

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