1 #ifdef _WIN32
2 #include <Windows.h>
3 #else
4 #include <dlfcn.h>
5 #include <unistd.h>
6 #endif
7 
8 #include <assert.h>
9 #include <stdio.h>
10 #include <thread>
11 #include <chrono>
12 
13 // We do not use the dylib.h implementation, because
14 // we need to pass full path to the dylib.
15 void* dylib_open(const char* full_path) {
16 #ifdef _WIN32
17   return LoadLibraryA(full_path);
18 #else
19   return dlopen(full_path, RTLD_LAZY);
20 #endif
21 }
22 
23 int main(int argc, char* argv[]) {
24   assert(argc == 2 && "argv[1] must be the full path to lib_b library");
25   const char* dylib_full_path= argv[1];
26   printf("Using dylib at: %s\n", dylib_full_path);
27 
28   // Wait until debugger is attached.
29   int main_thread_continue = 0;
30   int i = 0;
31   int timeout = 10;
32   for (i = 0; i < timeout; i++) {
33     std::this_thread::sleep_for(std::chrono::seconds(1));  // break here
34     if (main_thread_continue) {
35       break;
36     }
37   }
38   assert(i != timeout && "timed out waiting for debugger");
39 
40   // dlopen the 'liblib_b.so' shared library.
41   void* dylib_handle = dylib_open(dylib_full_path);
42   assert(dylib_handle && "dlopen failed");
43 
44   return i; // break after dlopen
45 }
46