diff --git a/src/libappimage/utils/DLHandle.h b/src/libappimage/utils/DLHandle.h index 8da21d3b..86942ff1 100644 --- a/src/libappimage/utils/DLHandle.h +++ b/src/libappimage/utils/DLHandle.h @@ -25,13 +25,37 @@ namespace appimage { * @param libName * @param mode */ - explicit DLHandle(const std::string& libName, int mode) : libName(libName) { + explicit DLHandle(const std::string& libName, int mode) : handle(nullptr), libName(libName) { handle = dlopen(libName.c_str(), mode); - if (!handle) + if (handle == nullptr) throw DLHandleError("Unable to load " + libName); } + /** + * Load one of the libraries listed in with the given flags. For details about + * the allowed flags see the dlopen doc. + * @param libName + * @param mode + */ + explicit DLHandle(std::initializer_list libNames, int mode) : handle(nullptr) { + for (const auto& item: libNames) { + handle = dlopen(item.c_str(), mode); + if (handle != nullptr) { + libName = item; + break; + } + } + + if (handle == nullptr) { + std::string libNamesStr; + for (const auto& item: libNames) + libNamesStr += " " + item; + + throw DLHandleError("Unable to load any of: " + libNamesStr); + } + } + virtual ~DLHandle() { dlclose(handle); }; diff --git a/src/libappimage/utils/IconHandle.cpp b/src/libappimage/utils/IconHandle.cpp index f83234cf..329cc850 100644 --- a/src/libappimage/utils/IconHandle.cpp +++ b/src/libappimage/utils/IconHandle.cpp @@ -224,14 +224,18 @@ namespace appimage { g_object_unref_t object_unref = nullptr; /** - * @brief Load libgobject-2.0.so and resolve the symbol addresses required by the IconHandle. + * @brief Load libgobject-2 and resolve the symbol addresses required by the IconHandle. + * + * Known library name by distribution: + * - CentOS: libgobject-2.0.so.0 + * - Debian/Ubuntu: libgobject-2.0.so * * Mode comments: * RTLD_LAZY - load the lib only the required symbols * RTLD_NODELETE - do not unload the lib, as it wasn't designed to be used this way it * will produce a big crash. */ - GLibOjbectHandle() : DLHandle("libgobject-2.0.so", RTLD_LAZY | RTLD_NODELETE) { + GLibOjbectHandle() : DLHandle({"libgobject-2.0.so", "libgobject-2.0.so.0"}, RTLD_LAZY | RTLD_NODELETE) { DLHandle::loadSymbol(object_unref, "g_object_unref"); } };