2

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")
Jepessen
  • 11,744
  • 14
  • 82
  • 149

2 Answers2

1

In my project I use a dedicated libs folder. What I have found is that I need the following DLLs:

  • libeay32.dll
  • libiconv-2.dll
  • libintl-8.dll
  • libpq.dll
  • ssleay32.dll

This was also verified with DependencyWalker, maybe on a project that demands more features you may need some other dll, but for basic SQL operation those were the set.

prmottajr
  • 1,816
  • 1
  • 13
  • 22
  • Is this list still valid for PG versions 10+? I'd like to update HeidiSQL's libraries, but for some reason with the v10.2 libpq.dll I get error 126: "module cannot be found". Probably there is a way to get an even more verbose error message here (using Delphi)? – Anse Feb 03 '20 at 18:57
  • 4
    To answer my own question: PG 10+ versions don't use libeay32.dll and ssleay32.dll any longer. Instead, I had to add libcrypto-1_1.dll and libssl-1_1.dll. See [here](https://github.com/HeidiSQL/HeidiSQL/issues/831) for more details. – Anse Feb 03 '20 at 20:03
  • 1
    libpq.dll, libssl-1_1-x64.dll, libintl-9.dll, libcrypto-1_1-x64.dll, libiconv-2.dll were the ones I needed for PG14 – Dan Mar 12 '22 at 21:40
1

Not sure if this resolves your problem, but it worked for me:

find_package(PostgreSQL REQUIRED)
include_directories (${PostgreSQL_INCLUDE_DIRS})
link_directories(${PostgreSQL_LIBRARY_DIRS})

Also, this solves the problem that I had when I got to this question, so hopefully this will help someone else.

Gabriele
  • 420
  • 4
  • 15