4

My problem is relatively simple to explain. I have a CMake project(using CLion) running mainly on Windows.

A main CMakeLists.txt project, linking 2 subdirectories

  • src/Library shared library
  • src/Executable executable linking Library

See the project structure below.

Project structure # main CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(TestTest)

set(CMAKE_CXX_STANDARD 11)

add_subdirectory(src/Library)
add_subdirectory(src/Executable)


# src/Library/CMakeLists.txt
add_subdirectory(include)
add_subdirectory(src)

add_library(TestLib SHARED ${TESTLIB_H_FILES} ${TESTLIB_SRC_FILES} )
target_include_directories(TestLib PUBLIC include)

#src/Executable/CMakeLists.txt

add_subdirectory(src)
add_executable(Executable ${EXECUTABLE_SRC_FILES})
target_link_libraries(Executable PUBLIC TestLib)

My problem is that the executable can't find the shared library at runtime.

I tried to add link_directories( ${CMAKE_CURRENT_BINARY_DIR}/src/Library) but didn't work.

What am I missing?

Please note: I wouldn't like to copy the shared library next to the executable manually/automatically by CMake, since I believe I would loose the debugging "capability" of the shared library. I will be getting the following error message by the GDB: No source file named C:/Users/flatron/CLionProjects/TestTest/src/Library/src/libr‌​ary.cpp.

All suggestions are really welcome and appreciated,

Thank you for your help

flatronka
  • 1,061
  • 25
  • 51
  • `My problem is that the executable can't find the shared library.` - Do you mean that when executable is running (**at runtime**), a loader cannot find the library? (This is opposite to the situation, when an executable is **failed to build** because linker cannot find the library). – Tsyvarev Nov 12 '17 at 21:05
  • You are right, I have the problem only at runtime, the executable builds fine. I edited the question. Thanks for your comment. – flatronka Nov 13 '17 at 07:30
  • Possible duplicate of [Building of executable and shared library with cmake, runtimelinker does not find dll](https://stackoverflow.com/questions/23323741/building-of-executable-and-shared-library-with-cmake-runtimelinker-does-not-fin) – Tsyvarev Nov 13 '17 at 07:40
  • Thanks for looking this up, I missed this. I hope there is a better solution for the problem, I agree with the last answer of the above mentioned thread, this is quite a poor solution.Moving all libs the the same folder wouldn't work neither since then I will loose the debugging the `Library` with breakpoints. This is quite of a simple problem there has to be a better simple solution I missed ;) – flatronka Nov 13 '17 at 07:55
  • Windows doesn't support "references" to libraries in an executable (this is known as "Runtime PATH", RPATH in other operating systems). As this is an **OS constraint**; neither CMake nor CLion simply cannot overcome it. BTW, you care about debugging; instead of asking in *general*, why do not ask **specifically** about the debugging? Note on the second answer to the referenced question: https://stackoverflow.com/a/26513002/3440745: it doesn't `copy the shared library next to the executable`, but it **creates** both library and executable in the same directory. So debugging shouldn't be broken. – Tsyvarev Nov 13 '17 at 08:42
  • The problem is that by that the GDB won't be able to find my sources, so I will be getting the following error message by the `GDB No source file named C:/Users/flatron/CLionProjects/TestTest/src/Library/src/library.cpp.` – flatronka Nov 13 '17 at 09:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158837/discussion-between-tsyvarev-and-flatronka). – Tsyvarev Nov 13 '17 at 09:26

1 Answers1

0

I suggest to use the following cmake command:

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

This will build all executables and shared libraries into the bin folder. So there is no need to copy anything by hand.

Soeren
  • 1,725
  • 1
  • 16
  • 30
  • Thank you for your answer Soeren. I tried this, but it somehow messes up the debugging, since it doesn't copy the symbols which should be included, will try to do some tests if I can make it work to stop at the breakpoints. – flatronka Nov 13 '17 at 09:05
  • The problem is that by that the GDB won't be able to find my sources, so I will be getting the following error message by the GDB `No source file named C:/Users/flatron/CLionProjects/TestTest/src/Library/src/libr‌​ary.cpp.` – flatronka Nov 13 '17 at 09:09