Home > database >  How to manage features that depend on libraries that may or may not be installed?
How to manage features that depend on libraries that may or may not be installed?

Time:07-16

I'm writing some new functionality for a graphics program (written mostly in C, with small parts in C ). The new functionality will make use of libgmic. Some users of the program will have libgmic installed, quite a lot will not. The program is monolithic: I'm not writing a plugin, this will be part of the main program. Compiling the program with the right headers is easy, but I need to be able to check at runtime whether the library is installed on the user's system or not in order to enable / disable the particular menu item so that users without the library installed can't invoke this piece of functionality and crash the program. What's the best way of going about this?

CodePudding user response:

You need to load the library at runtime with dlopen (or LoadLibrary on Windows) instead of linking to it, get function pointers with dlsym (GetProcAddress on Windows) and use them instead of function prototypes from the headers. Otherwise your program will simply fail to startup without the library (or crash, in some cases).

Some libraries support such usage well, such as providing types for all the functions you need. With others you’re on your own but that’s still possible.

  • Related