CMake shared library in subdirectory

Viewed 3273

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

1 Answers

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.

Related