Проблемы с созданием проекта с использованием libpqx
Я пытаюсь разработать проект с использованием библиотеки libpqxx на c++, с postgresql работал только на python. Это первый проект, где я использую стороннюю библиотеку для c++, поэтому я не особо понимаю, как правильно импортировать их в проект, я использую vscode. Подскажите как правильно это сделать. В данный момент у меня только main.cpp
#include <iostream>
#include <pqxx/pqxx>
int main() {
try {
// Создание строки подключения
std::string connectionString = "host=localhost port=5432 dbname=Dota user=postgres password=1q2ws3edc4r";
// Создание объекта подключения
pqxx::connection C(connectionString.c_str());
if (C.is_open()) {
std::cout << "Открыто подключение к базе данных: " << C.dbname() << std::endl;
// Создание транзакции
pqxx::work W(C);
// Выполнение SQL-запроса
pqxx::result R = W.exec("SELECT version();");
// Вывод результатов
for (auto row: R) {
std::cout << "PostgreSQL version: " << row[0].c_str() << std::endl;
}
// Завершение транзакции
W.commit();
} else {
std::cout << "Не удалось открыть подключение к базе данных" << std::endl;
}
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
и CMakeLists.txt
include(FetchContent)
cmake_minimum_required (VERSION 3.10)
project(sqltest VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(BUILD_TEST FALSE CACHE BOOL "Build tests")
set(PQXX_ENABLE_STATIC TRUE CACHE BOOL "Enable static libpqxx")
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}")
проект собирается, но ничего не выводит
, если я убираю
set(BUILD_TEST FALSE CACHE BOOL "Build tests")
set(PQXX_ENABLE_STATIC TRUE CACHE BOOL "Enable static libpqxx")
из CMakeLists.txt, то проест вовсе перестает запускаться. Пытался еще импортировать в проект уже собранную библиотеку, но не понял как.
лог сборки
[main] Сборка папки: sqltest all
[build] Запуск сборки.
[proc] Выполнение команды: chcp
[proc] Выполнение команды: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/sqltest/build --config Debug --target all -j 14 --
[build] [ 1%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/array.cxx.obj
[build] [ 2%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/binarystring.cxx.obj
[build] [ 4%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/connection.cxx.obj
[build] [ 5%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/connection_base.cxx.obj
[build] [ 7%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/cursor.cxx.obj
[build] [ 8%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/dbtransaction.cxx.obj
[build] [ 10%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/encodings.cxx.obj
[build] [ 14%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/connection.cxx.obj
[build] [ 14%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/binarystring.cxx.obj
[build] [ 14%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/array.cxx.obj
[build] [ 15%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/connection_base.cxx.obj
[build] [ 17%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/cursor.cxx.obj
[build] [ 20%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/encodings.cxx.obj
[build] [ 20%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/dbtransaction.cxx.obj
[build] [ 21%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/errorhandler.cxx.obj
[build] [ 23%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/errorhandler.cxx.obj
[build] [ 24%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/except.cxx.obj
[build] [ 26%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/field.cxx.obj
[build] [ 27%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/except.cxx.obj
[build] [ 28%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/field.cxx.obj
[build] [ 30%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/largeobject.cxx.obj
[build] [ 31%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/largeobject.cxx.obj
[build] [ 33%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/nontransaction.cxx.obj
[build] [ 34%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/notification.cxx.obj
[build] [ 36%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/nontransaction.cxx.obj
[build] [ 37%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/pipeline.cxx.obj
[build] [ 39%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/prepared_statement.cxx.obj
[build] [ 40%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/notification.cxx.obj
[build] [ 42%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/result.cxx.obj
[build] [ 44%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/prepared_statement.cxx.obj
[build] [ 44%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/pipeline.cxx.obj
[build] [ 46%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/result.cxx.obj
[build] [ 47%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/robusttransaction.cxx.obj
[build] [ 49%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/row.cxx.obj
[build] [ 50%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/robusttransaction.cxx.obj
[build] [ 52%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/row.cxx.obj
[build] [ 53%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/sql_cursor.cxx.obj
[build] [ 55%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/statement_parameters.cxx.obj
[build] [ 56%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/strconv.cxx.obj
[build] [ 57%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/sql_cursor.cxx.obj
[build] [ 59%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/stream_base.cxx.obj
[build] [ 60%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/statement_parameters.cxx.obj
[build] [ 62%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/stream_to.cxx.obj
[build] [ 63%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/stream_from.cxx.obj
[build] [ 65%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/strconv.cxx.obj
[build] [ 66%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/subtransaction.cxx.obj
[build] [ 68%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/stream_base.cxx.obj
[build] [ 69%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/stream_from.cxx.obj
[build] [ 71%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/stream_to.cxx.obj
[build] [ 72%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/tablereader.cxx.obj
[build] [ 73%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/tablestream.cxx.obj
[build] [ 75%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/tablereader.cxx.obj
[build] [ 76%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/subtransaction.cxx.obj
[build] [ 78%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/tablestream.cxx.obj
[build] [ 79%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/transaction.cxx.obj
[build] [ 81%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/tablewriter.cxx.obj
[build] [ 82%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/tablewriter.cxx.obj
[build] [ 84%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/transaction_base.cxx.obj
[build] [ 85%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/transaction.cxx.obj
[build] [ 86%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/util.cxx.obj
[build] [ 88%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_static.dir/version.cxx.obj
[build] [ 89%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/transaction_base.cxx.obj
[build] [ 91%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/util.cxx.obj
[build] [ 92%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/version.cxx.obj
[build] [ 94%] Building CXX object _deps/libpqxx-build/src/CMakeFiles/pqxx_shared.dir/__/win32/libpqxx.cxx.obj
[build] C:/sqltest/build/_deps/libpqxx-src/src/connection_base.cxx: In function 'void pqxx::internal::wait_read(const pq::PGconn*, long int, long int)':
[build] C:/sqltest/build/_deps/libpqxx-src/src/connection_base.cxx:1299:18: warning: narrowing conversion of '(time_t)seconds' from 'time_t' {aka 'long long int'} to 'long int' [-Wnarrowing]
[build] 1299 | timeval tv = { time_t(seconds), int(microseconds) };
[build] | ^~~~~~~~~~~~~~~
[build] C:/sqltest/build/_deps/libpqxx-src/src/connection_base.cxx: In function 'void pqxx::internal::wait_read(const pq::PGconn*, long int, long int)':
[build] C:/sqltest/build/_deps/libpqxx-src/src/connection_base.cxx:1299:18: warning: narrowing conversion of '(time_t)seconds' from 'time_t' {aka 'long long int'} to 'long int' [-Wnarrowing]
[build] 1299 | timeval tv = { time_t(seconds), int(microseconds) };
[build] | ^~~~~~~~~~~~~~~
[build] [ 95%] Linking CXX static library libpqxx.a
[build] [ 97%] Linking CXX shared library libpqxx.dll
[build] [ 97%] Built target pqxx_static
[build] [ 98%] Building CXX object CMakeFiles/main.dir/main.cpp.obj
[build] [100%] Linking CXX executable main.exe
[build] [100%] Built target main
[build] [100%] Built target pqxx_shared
[driver] Сборка выполнена: 00:00:22.296
[build] Сборка выполнена с кодом выхода 0.