How to add libpqxx library to cmake?
Asked Answered
H

2

6

I am working on my C++ simple project. I faced a crucial problem in CMakelists.txt. I cannot link libpqxx library correctly.

I am using

  • C++17
  • CMake 3.10
  • Ubuntu 16.04
  • CLion

This my CMakelists.txt:

cmake_minimum_required(VERSION 3.10)
project(myserver)

set(CMAKE_CXX_STANDARD 17)

find_package(Boost)
IF (Boost_FOUND)
   include_directories(${Boost_INCLUDE_DIR})
endif()

link_directories(/usr/local/lib)
include_directories(/usr/local/include)

file(GLOB_RECURSE SOURCE_FILES "src/*.cpp")

add_executable(myserver ${SOURCE_FILES})

TARGET_LINK_LIBRARIES(myserver  ${LIBRARIES})
Haletky answered 1/8, 2018 at 12:54 Comment(0)
B
4

libpqxx with cmake example without building libpqxx in cmake

Setup environment

1. Install CMake

cd ~
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
tar xf cmake-3.14.5.tar.gz
cd cmake-3.14.5
./bootstrap --parallel=10
make -j4
sudo make -j4 install

2. Install libpqxx

cd ~
git clone https://github.com/jtv/libpqxx.git
./configure
make -j4
sudo make install

How to build

mkdir build
cd build
cmake ..
cmake --build .

How to run

./main

Opened database successfully: postgres

Project

CMakeLists.txt

cmake_minimum_required (VERSION 3.10)
project(example VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(main main.cpp)

target_link_libraries(main -lpqxx -lpq)

main.cpp

#include <iostream>
#include <pqxx/pqxx>

using namespace std;
using namespace pqxx;

int main(int argc, char *argv[])
{
    try
    {
        connection C("dbname = postgres user = postgres password = mysecretpassword \
      hostaddr = 127.0.0.1 port = 5433");
        if (C.is_open())
        {
            cout << "Opened database successfully: " << C.dbname() << endl;
        }
        else
        {
            cout << "Can't open database" << endl;
            return 1;
        }
    }
    catch (const std::exception &e)
    {
        cerr << e.what() << std::endl;
        return 1;
    }
}

libpqxx with cmake example with building libpqxx in cmake

Setup environment

1. Install CMake

cd ~
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
tar xf cmake-3.14.5.tar.gz
cd cmake-3.14.5
./bootstrap --parallel=10
make -j4
sudo make -j4 install

How to build

mkdir build
cd build
cmake ..
cmake --build .

How to run

./main

Opened database successfully: postgres

Project

CMakeLists.txt

include(FetchContent)

cmake_minimum_required (VERSION 3.10)
project(example VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

FetchContent_Declare(
  libpqxx
  GIT_REPOSITORY https://github.com/jtv/libpqxx.git
  GIT_TAG a6b1d60e74c1427c8ac2324b85cd4a0dc2068332
)
set(PQXX_LIBRARIES pqxx_static)

FetchContent_MakeAvailable(libpqxx)

add_executable(main main.cpp)

target_link_libraries(main "${PQXX_LIBRARIES}")



main.cpp

#include <iostream>
#include <pqxx/pqxx>

using namespace std;
using namespace pqxx;

int main(int argc, char *argv[])
{
    try
    {
        connection C("dbname = postgres user = postgres password = mysecretpassword \
      hostaddr = 127.0.0.1 port = 5433");
        if (C.is_open())
        {
            cout << "Opened database successfully: " << C.dbname() << endl;
        }
        else
        {
            cout << "Can't open database" << endl;
            return 1;
        }
    }
    catch (const std::exception &e)
    {
        cerr << e.what() << std::endl;
        return 1;
    }
}

Source code: libpqxx with cmake example

Both answered 3/8, 2021 at 9:39 Comment(1)
FetchContent is helpful; however on ubuntu 22.04, I also had to sudo apt-get install libpq-devNeoarsphenamine
H
10

I found the solution.

cmake_minimum_required(VERSION 3.10)

project(myserver)

set(CMAKE_CXX_STANDARD 17)

find_package(Boost)
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIR})
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lpqxx -lpq")

set(PQXX /usr/local/include/pqxx)

find_library(PQXX_LIB pqxx)
find_library(PQ_LIB pq)

file(GLOB_RECURSE SOURCE_FILES "src/*.cpp")

add_executable(myserver ${SOURCE_FILES})

TARGET_LINK_LIBRARIES(myserver ${PQXX_LIB} ${PQ_LIB})
Haletky answered 1/8, 2018 at 18:21 Comment(1)
Is it better to use find package in this situation? I have a similar issue which I posted in a different question. #56696547. The recommendation was to use a find file. Is there any reason you did not do that here?Haulage
B
4

libpqxx with cmake example without building libpqxx in cmake

Setup environment

1. Install CMake

cd ~
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
tar xf cmake-3.14.5.tar.gz
cd cmake-3.14.5
./bootstrap --parallel=10
make -j4
sudo make -j4 install

2. Install libpqxx

cd ~
git clone https://github.com/jtv/libpqxx.git
./configure
make -j4
sudo make install

How to build

mkdir build
cd build
cmake ..
cmake --build .

How to run

./main

Opened database successfully: postgres

Project

CMakeLists.txt

cmake_minimum_required (VERSION 3.10)
project(example VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(main main.cpp)

target_link_libraries(main -lpqxx -lpq)

main.cpp

#include <iostream>
#include <pqxx/pqxx>

using namespace std;
using namespace pqxx;

int main(int argc, char *argv[])
{
    try
    {
        connection C("dbname = postgres user = postgres password = mysecretpassword \
      hostaddr = 127.0.0.1 port = 5433");
        if (C.is_open())
        {
            cout << "Opened database successfully: " << C.dbname() << endl;
        }
        else
        {
            cout << "Can't open database" << endl;
            return 1;
        }
    }
    catch (const std::exception &e)
    {
        cerr << e.what() << std::endl;
        return 1;
    }
}

libpqxx with cmake example with building libpqxx in cmake

Setup environment

1. Install CMake

cd ~
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
tar xf cmake-3.14.5.tar.gz
cd cmake-3.14.5
./bootstrap --parallel=10
make -j4
sudo make -j4 install

How to build

mkdir build
cd build
cmake ..
cmake --build .

How to run

./main

Opened database successfully: postgres

Project

CMakeLists.txt

include(FetchContent)

cmake_minimum_required (VERSION 3.10)
project(example VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

FetchContent_Declare(
  libpqxx
  GIT_REPOSITORY https://github.com/jtv/libpqxx.git
  GIT_TAG a6b1d60e74c1427c8ac2324b85cd4a0dc2068332
)
set(PQXX_LIBRARIES pqxx_static)

FetchContent_MakeAvailable(libpqxx)

add_executable(main main.cpp)

target_link_libraries(main "${PQXX_LIBRARIES}")



main.cpp

#include <iostream>
#include <pqxx/pqxx>

using namespace std;
using namespace pqxx;

int main(int argc, char *argv[])
{
    try
    {
        connection C("dbname = postgres user = postgres password = mysecretpassword \
      hostaddr = 127.0.0.1 port = 5433");
        if (C.is_open())
        {
            cout << "Opened database successfully: " << C.dbname() << endl;
        }
        else
        {
            cout << "Can't open database" << endl;
            return 1;
        }
    }
    catch (const std::exception &e)
    {
        cerr << e.what() << std::endl;
        return 1;
    }
}

Source code: libpqxx with cmake example

Both answered 3/8, 2021 at 9:39 Comment(1)
FetchContent is helpful; however on ubuntu 22.04, I also had to sudo apt-get install libpq-devNeoarsphenamine

© 2022 - 2024 — McMap. All rights reserved.