1

Normal compilation (works fine):

g++ DBHandler.cpp Functions.cpp Main.cpp -I/usr/local/include -L/usr/local/lib -lconfig++ -lpqxx -lpq -o dbhandler

It`s possible to run:

./dbhandler

CMakeLists.txt (standard):

cmake_minimum_required(VERSION 2.8.9)
project(DBHandler)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
target_link_libraries(dbhandler config++ pqxx pq)
add_executable(dbhandler ${SOURCES})

How to change CMakeLists.txt and add:

-I/usr/local/include -L/usr/local/lib -lconfig++ -lpqxx -lpq

to compile program using cmake?

user3455638
  • 569
  • 1
  • 6
  • 17
  • 1
    Possible duplicate of [How to add "-l" (ell) compiler flag in CMake](https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake) – Tsyvarev Jun 05 '17 at 22:22
  • I think you need to create the target first by calling add_executable and later specify the libraries with target_link_libraries. – vre Jun 06 '17 at 08:13

1 Answers1

0

The correct solution is to use the CMake package PkgConfig to use pkg_search_module

Your file will become:

cmake_minimum_required(VERSION 2.8.9)
project(DBHandler)

find_package(PkgConfig REQUIRED)

pkg_search_module(CONFIGPP REQUIRED config++)
pkg_search_module(PQ REQUIRED pq)
pkg_search_module(PQXX REQUIRED pqxx)

include_directories(include ${CONFIGPP_INCLUDE_DIRS} ${PQ_INCLUDE_DIRS} ${PQXX_INCLUDE_DIRS})
file(GLOB SOURCES "src/*.cpp")
target_link_libraries(dbhandler ${CONFIGPP_LIBRARIES} ${PQ_LIBRARIES} ${PQXX_LIBRARIES})
add_executable(dbhandler ${SOURCES})
OlivierM
  • 2,820
  • 24
  • 41
  • CMake Error at /usr/share/cmake-3.7/Modules/FindPkgConfig.cmake:637 (message): None of the required 'pqxx' found Libraries were installed by sudo apt-get install ... not from the sources. Maybe, this is the problem of lack findlib.cmake-s files? – user3455638 Jun 06 '17 at 22:24
  • You should install the package `libpqxx-dev` with `sudo apt-get install libpqxx-dev` – OlivierM Jun 06 '17 at 22:35