Skip to content

Commit

Permalink
added os-specific library name handling and Mac results
Browse files Browse the repository at this point in the history
  • Loading branch information
wawiesel committed Jul 17, 2017
1 parent 19b6207 commit 70eb77d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ cmake ..
cmake --build .
```

Run tests:
Run tests using Linux:
```bash
for i in ./main_A.* ./main_B.* ./main_AB.*; do echo "$i:"; LD_LIBRARY_PATH=./A:./B:$LD_LIBRARY_PATH $i; done
```

Run tests using Mac:
```bash
for i in ./main_A.* ./main_B.* ./main_AB.*; do echo "$i:"; DYLD_LIBRARY_PATH=./A:./B:$DYLD_LIBRARY_PATH $i; done
```

## Results

Ubuntu 16.04.02, CentOS 7.3: result the same for both GCC and Clang:
Expand All @@ -60,3 +65,26 @@ Ubuntu 16.04.02, CentOS 7.3: result the same for both GCC and Clang:
main_AB->call_A->call_C (v1) # <- correct
main_AB->call_B->call_C (v1) # <- wrong
```

MacOSX El Capitan 10.11.6, result same for both GCC and Clang:
```
./main_A.shared:
main_A->call_A->call_C (v1)
./main_A.static:
main_A->call_A->call_C (v1)
./main_B.shared:
main_B->call_B->call_C (v2)
./main_B.static:
main_B->call_B->call_C (v2)
./main_AB.dlopen:
main_AB->call_A->call_C (v1) # <- correct
main_AB->call_B->call_C (v2) # <- correct
./main_AB.shared:
main_AB->call_A->call_C (v1) # <- correct
main_AB->call_B->call_C (v2) # <- correct
./main_AB.static:
main_AB->call_A->call_C (v1) # <- correct
main_AB->call_B->call_C (v1) # <- wrong
```


24 changes: 22 additions & 2 deletions main_AB.dlopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@
// #define DLOPEN_FLAGS RTLD_LAZY | RTLD_LOCAL
#define DLOPEN_FLAGS RTLD_LAZY

#if defined(_WIN32) || defined(__CYGWIN__)
// Windows (x86 or x64)
const char* libA = "libA.shared.dll";
const char* libB = "libB.shared.dll";
#elif defined(__linux__)
// Linux
const char* libA = "libA.shared.so";
const char* libB = "libB.shared.so";
#elif defined(__APPLE__) && defined(__MACH__)
// Mac OS
const char* libA = "libA.shared.dylib";
const char* libB = "libB.shared.dylib";
#elif defined(unix) || defined(__unix__) || defined(__unix)
// Unix like OS
const char* libA = "libA.shared.so";
const char* libB = "libB.shared.so";
#else
#error Unknown environment!
#endif

int main(int argc, char **argv)
{
(void)argc;
Expand All @@ -17,13 +37,13 @@ int main(int argc, char **argv)
int (*call_B)(void);
char *error;

handle_B = dlopen("libB.shared.so", DLOPEN_FLAGS);
handle_B = dlopen(libB, DLOPEN_FLAGS);
if(handle_B == NULL) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}

handle_A = dlopen("libA.shared.so", DLOPEN_FLAGS);
handle_A = dlopen(libA, DLOPEN_FLAGS);
if(handle_A == NULL) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
Expand Down

0 comments on commit 70eb77d

Please sign in to comment.