スポンサーリンク
書いたつもりだがサイト内検索しても出てこないのでまだやってないらしい。
#include <iostream> #include <functional> #include <Windows.h> int main() { HMODULE hModule = LoadLibraryA("MyDLL.dll"); FARPROC func = GetProcAddress(hModule, "add"); std::function<int(int, int)> add_func = reinterpret_cast<int(*)(int, int)>(func); int result = add_func(3, 5); std::cout << "result: " << result << std::endl; FreeLibrary(hModule); return 0; }
extern "C" { int add(int a, int b) { return a + b; } }
#include <iostream> #include <functional> #include <dlfcn.h> int main(){ void* handle = dlopen("./mylib.so",RTLD_LAZY); using add_func_ptr = int (*)(int,int); std::function<int(int,int)> add_func =(add_func_ptr)dlsym(handle,"add"); int result = add_func(3,5); std::cout << "result:" << result << std::endl; dlclose(handle); }
さすがに記事が寂しすぎるのでWindowsとLinux版のコードを載せた。
あとMacOSでは拡張子を.dylibにすればよいらしい。