CMake and Dylib: Character Definitions

I am encountering some difficulties in configuring cmake for dylib.

Here is my test:

mylibfunc.cpp

#include <stdio.h> static int count = 0; extern "C" { int mylibfunc() { count++; return count; } } 

basictest.cpp

 #include <stdio.h> #include <dlfcn.h> #include <mach-o/dyld.h> typedef int (*funcPtr)(); int main() { // Load first library void* handleA = dlopen("libmylib.dylib", RTLD_LAZY); funcPtr functionA = (int(*)())dlsym(handleA, "mylibfunc"); fprintf(stderr, "Handle A: %p\tFunction A: %p\t Count: %d\n", handleA, functionA, (*functionA)()); // Reload same library void* handleB = dlopen("libmylib.dylib", RTLD_LAZY); funcPtr functionB = (int(*)())dlsym(handleB, "mylibfunc"); fprintf(stderr, "Handle B: %p\tFunction B: %p\t Count: %d\n", handleB, functionB, (*functionB)()); // Load copy of first library (just rename) void* handleC = dlopen("libmylib_copy.dylib", RTLD_LAZY); funcPtr functionC = (int(*)())dlsym(handleC, "mylibfunc"); fprintf(stderr, "Handle C: %p\tFunction C: %p\t Count: %d\n", handleC, functionC, (*functionC)()); return 0; } 

CMakeLists:

 cmake_minimum_required(VERSION 2.8.11) SET(src_dir mylibfunc.cpp) add_library(mylib SHARED ${src_dir}) target_link_libraries(mylib ${EXTRA_LIBS}) 

Test 1 on the command line:

 clang++ -dynamiclib mylibfunc.cpp -o libmylib.dylib cp libmylib.dylib libmylib_copy.dylib clang++ basictest.cpp -o basictest ./basictest 

Output:

 Handle A: 0x7fba614039b0 Function A: 0x10f7a5f50 Count: 1 Handle B: 0x7fba614039b0 Function B: 0x10f7a5f50 Count: 2 Handle C: 0x7fba61403de0 Function C: 0x10f7d8f50 Count: 1 

-> Each lib has its own article counter, works great.

Test 2 With cmake:

 cmake -G"Xcode" open Project.xcodeproj and build project on xcode 4 cp libmylib.dylib libmylib_copy.dylib clang++ basictest.cpp -o basictest ./basictest 

Output:

 Handle A: 0x7ff5424039b0 Function A: 0x104a63f50 Count: 1 Handle B: 0x7ff5424039b0 Function B: 0x104a63f50 Count: 2 Handle C: 0x7ff5424039b0 Function C: 0x104a63f50 Count: 3 

-> Each lib shared the same counter, which I don't want ....

What should I change in the cmake or xcode properties?

+4
source share
1 answer

I will try to give you the answer as complete as possible, but there are a few points that I do not quite understand, therefore ...

Firstly, the difference between manually created libraries and those created by CMake. The identifier of the first is a relative path, when it is absolute for a later one, you can see that with otool :

 otool -D libmylib.dylib 

This will be /something/libmylib.dylib for CMake built and libmylib.dylib for manual build. Of course, when copying dylib, the identifier remains unchanged.

So it seems that for some reason (yes, this part that I really don’t understand) when using absolute paths, the dynamic loader realizes that both dylib files are the same because they have the same identifier and not when the id is relative.

So, if you want to fix your problem, you should update the ID of the copied library. This can be done using install_name_tool :

 install_name_tool -id "libmylib_copy.dylib" libmylib_copy.dylib 
+2
source

Source: https://habr.com/ru/post/1488053/


All Articles