How can I conditionality load and use a shared library if it is installed, but still run without that functionality if it is not there? more specifically, can I do that without using that library as a plugin? I prefer failing during build time rather than runtime if possible.
if I build it with the library flag -lfoo, it builds. But then it fails to run it libfoo.so.2 is not installed in the target system. But if I don't add the library flag it fails in linking.
here is some code snipets for better picture.
myAdapter.cpp
#include "newlib/foo.h" //this is from the shared library
...
bool myAdapter::isAvailable()
{
handle_ = dlopen("libfoo.so.2", RTLD_LAZY);
if (!handle_)
{
return false;
}
return true;
}
...
bool myAdapter::init()
{
if (!isAvailable())
{
return false;
}
isInitilized = false;
isConnected = false;
if (!fooInit()) // shared library function
{
fooCleanup(); //shared library function
return false;
}
// these are my private functions but they call shared library functions.
if (!createUserParams_() || !setCallbacks_() || !createContext_() || !connect_())
{
return false;
}
return true;
}
...
myApp.cpp
#include "myAdapter.h"
...
int main()
{
...
foo = new myAdapter();
if (!foo.init())
{
cout << "Foo function is not available;
isFooAvailable = false;
}
}
...
}