将 libpqxx 库添加到 cmake
Posted
技术标签:
【中文标题】将 libpqxx 库添加到 cmake【英文标题】:Add libpqxx library to cmake 【发布时间】:2018-08-01 12:54:36 【问题描述】:我正在做我的 c++ 简单项目,我在 CMakelists.txt 中遇到了一个关键问题,我无法以错误的方式链接 libpqxx 库,我花了两天时间解决了这个问题。 我正在使用 C++ 2017, cmake 3.10版, 操作系统 ubuntu 16.04, 克利翁 这是我的 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)
提前致谢
【问题讨论】:
【参考方案1】:我找到了解决办法。
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)
谢谢
【讨论】:
在这种情况下使用 find 包会更好吗?我有一个类似的问题,我在另一个问题中发布。 ***.com/questions/56696047/…。建议是使用查找文件。你有什么理由不在这里做吗?【参考方案2】:带有 cmake 的 libpqxx 示例,无需在 cmake 中构建 libpqxx
设置环境
1.安装 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。安装 libpqxx
cd ~
git clone https://github.com/jtv/libpqxx.git
./configure
make -j4
sudo make install
如何构建
mkdir build
cd build
cmake ..
cmake --build .
如何运行
./main
Opened database successfully: postgres
项目
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 与 cmake 示例,在 cmake 中构建 libpqxx
设置环境
1.安装 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
如何构建
mkdir build
cd build
cmake ..
cmake --build .
如何运行
./main
Opened database successfully: postgres
项目
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;
源代码:libpqxx with cmake example
【讨论】:
以上是关于将 libpqxx 库添加到 cmake的主要内容,如果未能解决你的问题,请参考以下文章
使用libpqxx访问PostgreSQL数据库(mingw编译libpqxx)
使用vs2015编译器编译libpqxx时发生的错误以及解决办法