I'll try to give you an answer as complete as possible, but there's a few point I'm not sure to fully understand, so...
First, the difference between the libraries built "manually" and those built by CMake. The identifier of the former is a relative path when it's an absolute one for the later, you can see that with otool
:
otool -D libmylib.dylib
That'll be /something/libmylib.dylib
for the CMake built and libmylib.dylib
for the manual built.
Of course, when you copy the dylib, the identifier stay the same.
So, it appears that for some reason (yeah, that's the part I don't really understand) when using absolute paths, the dynamic loader understand that both the dylib files are the same because they have the same id, and not when the id is relative.
So, if you want to fix your problem, you should update the identifier of the copied library. That can be done with install_name_tool
:
install_name_tool -id "libmylib_copy.dylib" libmylib_copy.dylib