| 1 | #include <dlfcn.h> |
| 2 | #include <stdexcept> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | int twelve(int i) { |
| 6 | return 12 + i; // break 12 |
| 7 | } |
| 8 | |
| 9 | int thirteen(int i) { |
| 10 | return 13 + i; // break 13 |
| 11 | } |
| 12 | |
| 13 | namespace a { |
| 14 | int fourteen(int i) { |
| 15 | return 14 + i; // break 14 |
| 16 | } |
| 17 | } // namespace a |
| 18 | int main(int argc, char const *argv[]) { |
| 19 | #if defined(__APPLE__) |
| 20 | const char *libother_name = "libother.dylib" ; |
| 21 | #else |
| 22 | const char *libother_name = "libother.so" ; |
| 23 | #endif |
| 24 | |
| 25 | void *handle = dlopen(file: libother_name, RTLD_NOW); |
| 26 | if (handle == nullptr) { |
| 27 | fprintf(stderr, format: "%s\n" , dlerror()); |
| 28 | exit(status: 1); |
| 29 | } |
| 30 | |
| 31 | const char *message = "Hello from main!" ; |
| 32 | int (*foo)(int) = (int (*)(int))dlsym(handle: handle, name: "foo" ); |
| 33 | if (foo == nullptr) { |
| 34 | fprintf(stderr, format: "%s\n" , dlerror()); |
| 35 | exit(status: 2); |
| 36 | } |
| 37 | foo(12); // before loop |
| 38 | |
| 39 | for (int i = 0; i < 10; ++i) { |
| 40 | int x = twelve(i) + thirteen(i) + a::fourteen(i); // break loop |
| 41 | } |
| 42 | printf(format: "%s\n" , message); |
| 43 | try { |
| 44 | throw std::invalid_argument("throwing exception for testing" ); |
| 45 | } catch (...) { |
| 46 | puts(s: "caught exception..." ); |
| 47 | } |
| 48 | return 0; // after loop |
| 49 | } |
| 50 | |