From 6e02e3707a19f8f0aef00a0e83778ff199bb61ac Mon Sep 17 00:00:00 2001 From: Jean-Louis Leroy Date: Thu, 11 Aug 2016 16:30:13 -0400 Subject: [PATCH] Make dynamic loading example work on OSX. --- examples/CMakeLists.txt | 6 +++++- examples/dl_main.cpp | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index be279e1..ba9d55e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -20,7 +20,11 @@ add_executable(matrix matrix.cpp) target_link_libraries (matrix yomm11) if(NOT MSVC) - add_library( dl_shared SHARED dl_shared.cpp ) add_executable(dl_main dl_main.cpp) + add_library( dl_shared SHARED dl_shared.cpp ) + if(APPLE) + set_target_properties(dl_main PROPERTIES LINK_FLAGS "-Wl,-export_dynamic") + set_target_properties(dl_shared PROPERTIES LINK_FLAGS "-Wl,-undefined,dynamic_lookup") + endif() target_link_libraries (dl_main yomm11 dl dl_shared) endif() diff --git a/examples/dl_main.cpp b/examples/dl_main.cpp index b123db8..ab92f02 100644 --- a/examples/dl_main.cpp +++ b/examples/dl_main.cpp @@ -36,7 +36,13 @@ int main() { cout << "encounter(Cow(), Wolf()) -> " << encounter(Cow(), Wolf()) << endl; cout << "encounter(Wolf(), Cow()) -> " << encounter(Wolf(), Cow()) << endl; - void* handle = dlopen("./libdl_shared.so", RTLD_NOW); + void* handle = dlopen( +#ifdef __APPLE__ + "./libdl_shared.dylib" +#else + "./libdl_shared.so" +#endif + , RTLD_NOW); if (!handle) { cout << "dlopen() failed: " << dlerror() << "\n";