I am trying to link to a shared library using CMake. I understand that this can bee achieved using the target_link_libraries() command. I have been able to get this working but not quite in the way that I would like.
Using the below CMakeLists.txt, I've been able to link to the shared library.
cmake_minimum_required(VERSION 3.7)
project(DylibTest)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(DylibTest ${SOURCE_FILES})
target_link_libraries(DylibTest
${CMAKE_SOURCE_DIR}/Resources/libDynamicTest.dylib)
This seems to tell the linker to look in the directory that the project is located in. This is fine for development, but how would I distribute this project? End-users will clearly not have this same folder.
I've tried to pass in a relative path like so:
cmake_minimum_required(VERSION 3.7)
project(DylibTest)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(DylibTest ${SOURCE_FILES})
target_link_libraries(DylibTest /Resources/libDynamicTest.dylib)
When doing this, I get the following error:
No rule to make target `/Resources/libDynamicTest.dylib', needed by `DylibTest'. Stop.
I've looked at the documentation for target_link_directories() but it has not been helpful. I've also looked at numerous other questions here.
In case it's not clear, my intention is to distribute the executable with a folder named "Resources" that contains the shared library. How should my CMakeLists.txt file look in order to do this?
EDIT: In response to this question being marked as a possible duplicated of this question, I do concede that that question is basically asking the same thing, the answers there do not work for me.
Here's how my CMakeLists.txt looks after trying a accepted answer on the other question:
cmake_minimum_required(VERSION 3.7)
project(DylibTest)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(DylibTest ${SOURCE_FILES})
link_directories(${CMAKE_BINARY_DIR}/Resources)
target_link_libraries(DylibTest DynamicTest)
This unfortunately does not work an results in an error stating that the library could not be found.