Reputation: 503
I have a shared library that I would like to load twice in my program. The use case is as following:
Is there a way from C/C++ code to do this? I am interested in a solution that works with gcc/g++
Upvotes: 3
Views: 275
Reputation: 2214
At first I was thinking about LoadLibrary, GetProcAddress and FreeLibrary but then I mentioned that you want gcc/g++
, which looks like you need *NIX solution. So I just have stolen solution from here:
loadlib.h
#ifndef __LOADLIB_H
#define __LOADLIB_H
#ifdef UNIX
#include <dlfcn.h>
#endif
#include <iostream>
using namespace std;
typedef void* (*funcPtr)();
#ifdef UNIX
# define IMPORT_DIRECTIVE __attribute__((__visibility__("default")))
# define CALL
#else
# define IMPORT_DIRECTIVE __declspec(dllimport)
# define CALL __stdcall
#endif
extern "C" {
IMPORT_DIRECTIVE void* CALL LoadLibraryA(const char* sLibName);
IMPORT_DIRECTIVE funcPtr CALL GetProcAddress(
void* hModule, const char* lpProcName);
IMPORT_DIRECTIVE bool CALL FreeLibrary(void* hLib);
}
#endif
Loadlib.cpp
#include "loadlib.h"
int main(int argc, char* argv[])
{
#ifndef UNIX
char* fileName = "hello.dll";
void* libraryHandle = LoadLibraryA(fileName);
if (libraryHandle == NULL)
cout << "dll not found" << endl;
else // make a call to "printHello" from the hello.dll
(GetProcAddress(libraryHandle, "printHello"))();
FreeLibrary(libraryHandle);
#else // unix
void (*voidfnc)();
char* fileName = "hello.so";
void* libraryHandle = dlopen(fileName, RTLD_LAZY);
if (libraryHandle == NULL)
cout << "shared object not found" << endl;
else // make a call to "printHello" from the hello.so
{
voidfnc = (void (*)())dlsym(libraryHandle, "printHello");
(*voidfnc)();
}
dlclose(libraryHandle);
#endif
return 0;
}
Upvotes: 4