I'm building a program with CMake on Windows. The program has a dependency on libpq
that's che C PostgreSQL library.
I load the PostgreSQL package, CMake find it and the program it's built. But then I want to copy libpq.dll
and its dependencies on the build folder in order to use it. I've copied libpq.dll
in a post build command, but it's not sufficient because it depends on other libraries.
What's the best way to copy libpq.dll
and all files that are needed to it in order to solve all dependencies?
cmake_minimum_required (VERSION 3.10)
project (postgresqldatabase)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
find_package (PostgreSQL REQUIRED)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories (${CMAKE_SOURCE_DIR}/src ${PostgreSQL_INCLUDE_DIRS})
file (GLOB_RECURSE PROJECT_SRC *.cpp)
add_library (${PROJECT_NAME} SHARED ${PROJECT_SRC})
target_link_libraries (${PROJECT_NAME} ${PostgreSQL_LIBRARIES})
# Copy PostgreSQL .dll files on bin folder if we are on Windows
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# It prints C:\Program Files\PostgreSQL\9.4\lib
message (STATUS "paths are ${PostgreSQL_LIBRARY_DIRS}")
# It prints C:/Program Files/PostgreSQL/9.4/lib/libpq.lib
message (STATUS "libraries are ${PostgreSQL_LIBRARIES}")
add_custom_command (TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PostgreSQL_LIBRARY_DIRS}/libpq.dll $<TARGET_FILE_DIR:${PROJECT_NAME}>)
# I need to copy also dll's needed to libpq.dll
endif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")