c++ - 包含 libpqxx 会导致使用 CMake 在 WSL 上构建失败

标签 c++ ubuntu cmake windows-subsystem-for-linux libpqxx

我目前正在升级使用 PostgreSQL 数据库的 C++ 应用程序。由于我们通常在基于 *nix 的系统上运行,所以我目前在 Windows Subsystem for Linux (WSL) 和 Ubuntu 发行版中工作。 PostgreSQL 10 是通过 apt-get 安装的,所有内容都在 following the directions to build 之前升级。类 Unix 系统上的 libpqxx

作为一个非常基本的测试,我有一个简单的类,仅包含引用:

#include <pqxx/pqxx> 

但是,构建结果会产生一个(长)错误日志,开头如下:

[ 15%] Building CXX object src/CMakeFiles/Core.dir/Reporters/DbReporter.cpp.o
In file included from /usr/local/include/pqxx/array.hxx:18:0,
                 from /usr/local/include/pqxx/array:4,
                 from /usr/local/include/pqxx/pqxx:2,
                 from /mnt/c/Users/User/git/Simulation/src/Reporters/DbReporter.cpp:3:
/usr/local/include/pqxx/internal/encodings.hxx:26:42: error: ‘pqxx::internal::encoding_group pqxx::internal::enc_group’ redeclared as different kind of symbol
 encoding_group enc_group(std::string_view);

随后出现的所有错误与 /pqxx/*. 下生成错误的文件具有相似的性质。通常,对于无效声明、函数不属于 std 命名空间(未在应用程序中重新定义)或看似语法错误的内容。

我怀疑 libpqxx 库的构建方式不匹配。如何隔离并纠正问题?

最佳答案

虽然构建的日志有点难以解析;但是,redeclared as different kind of symbol 行是一条线索,输出中还包含如下错误:

/usr/local/include/pqxx/stream_from.hxx: In constructor ‘pqxx::stream_from::stream_from(pqxx::transaction_base&, const string&, Iter, Iter ’:
/usr/local/include/pqxx/stream_from.hxx:134:19: error: missing template arguments before ‘(’ token
     separated_list(",", columns_begin, columns_end)

这些强烈暗示代码中存在语法错误,尽管库使用开发人员概述的工具链构建得很好。一个重要的一点是 libpqxx 依赖于 C++17。错误最终出现在 CMakeLists.txt 文件中:

set(CMAKE_CXX_STANDARD 14)

表明尽管该库依赖于 C++17,但该应用程序是使用 C++14 构建的。将行更新为 17 更正了问题并允许构建代码。此时 CMakeLists.txt must also be updated to link correctly :

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)

target_link_libraries(PROGRAM ${PQXX_LIB} ${PQ_LIB})

关于c++ - 包含 libpqxx 会导致使用 CMake 在 WSL 上构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57810153/

相关文章:

c++ - 使用常量修改重载成员运算符

windows - 如何在 CMake 中使用分号值创建一个列表?

c++ - 通过模板函数 C++ 的引用传递

c++ - 为什么我的可执行文件的 .dynstr 包含静态链接库中的符号?

ruby-on-rails - 如何修复 "Listen error: unable to monitor directories for changes"

linux - 在不升级的情况下恢复包状态以保持 Docker 容器较小

ubuntu - xclock - 总是最重要的功能?

c++ - CMake 编译时不包含链接库的路径

c++ - Cmake 重新生成变量变化

c++ - Lex 解析器 : (end of buffer or a NUL) segfault