1

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.

2 Answers2

4

Have a look at https://cmake.org/cmake/help/v3.0/command/find_library.html.

You can specify one or more alternative search folder for cmake to look for the library. It also looks in the default library folder for your OS as well.

find_library(YOURLIB 
      NAMES your // without the lib 
      PATHS ${CUSTOM_LIB_PATH})

You can then link the library with

target_link_library(targetname ${YOURLIB})
Stefan
  • 721
  • 1
  • 10
  • 24
  • This. `find_library` will also take care of appending the `.dylib` suffix for you, making your build more portable. Besides CMake variables you can also use environment variables as hints for library locations, which can be very handy, in particular if the same library has to be used by more than one project on the machine. Note though that this approach does *not* work if the library is being built as part of the same build as the depending executable, as the library file needs to be present when running CMake. – ComicSansMS Aug 28 '17 at 09:34
0

You might be looking for Creating packages with CPack? This lets you describe how your project shall be packaged into a setup routine and what the file layout after installing looks like.

Rumpel
  • 101
  • 3