c++ - 如何在简单的C++项目中使用Poco库?

标签 c++ cmake poco-libraries

这是我的项目的目录结构:

poco/
CMakeLists.txt
main.cpp

注意:poco目录包含使用以下命令从Poco的github存储库下载的所有文件:

git clone --recurse-submodules https://github.com/pocoproject/poco.git

这是CMakeLists.txt的内容:

cmake_minimum_required(VERSION 3.4.1)
add_executable(
    main
    main.cpp
)
add_subdirectory(poco)
include_directories(
   poco/ApacheConnector/include
   poco/CppParser/include
   poco/CppUnit/include
   poco/Crypto/include
   poco/Data/include
   poco/Encodings/include
   poco/Foundation/include
   poco/JSON/include
   poco/MongoDB/include
   poco/Net/include
   poco/NetSSL_OpenSSL/include
   poco/NetSSL_Win/include
   poco/openssl/build/include
   poco/PDF/include
   poco/Redis/include
   poco/SevenZip/include
   poco/Util/include
   poco/XML/include
   poco/Zip/include
)

target_link_libraries(main ${POCO_LIBRARIES})

这是main.cpp的内容:

#include <iostream>
#include <Poco/Net/HTTPClientSession.h>
using namespace std;
int main() {
    std::cout << "here";

    return 0;
}

运行这些命令后(在显示结构的目录中):

$mkdir _build && cd _build
$cmake ..
$make

我遇到这些错误:

[  0%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[  1%] Linking CXX executable main
CMakeFiles/main.dir/main.cpp.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::host() const':
/home/gandalf/Desktop/pocoTest/poco/Net/include/Poco/Net/SocketAddressImpl.h:143: undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, unsigned int, unsigned int)'
CMakeFiles/main.dir/main.cpp.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::~IPv6SocketAddressImpl()':
/home/gandalf/Desktop/pocoTest/poco/Net/include/Poco/Net/SocketAddressImpl.h:118: undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/main.dir/main.cpp.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::~IPv6SocketAddressImpl()':
/home/gandalf/Desktop/pocoTest/poco/Net/include/Poco/Net/SocketAddressImpl.h:118: undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/main.dir/main.cpp.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::~IPv4SocketAddressImpl()':
/home/gandalf/Desktop/pocoTest/poco/Net/include/Poco/Net/SocketAddressImpl.h:56: undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/main.dir/main.cpp.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::~IPv4SocketAddressImpl()':
/home/gandalf/Desktop/pocoTest/poco/Net/include/Poco/Net/SocketAddressImpl.h:56: undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/main.dir/main.cpp.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::host() const':
/home/gandalf/Desktop/pocoTest/poco/Net/include/Poco/Net/SocketAddressImpl.h:81: undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, unsigned int)'
CMakeFiles/main.dir/main.cpp.o:(.data.rel.ro._ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
CMakeFiles/main.dir/main.cpp.o:(.data.rel.ro._ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
CMakeFiles/main.dir/main.cpp.o:(.data.rel.ro._ZTVN4Poco3Net4Impl21IPv4SocketAddressImplE[_ZTVN4Poco3Net4Impl21IPv4SocketAddressImplE]+0x50): undefined reference to `Poco::Net::Impl::IPv4SocketAddressImpl::toString[abi:cxx11]() const'
CMakeFiles/main.dir/main.cpp.o:(.data.rel.ro._ZTVN4Poco3Net4Impl21IPv6SocketAddressImplE[_ZTVN4Poco3Net4Impl21IPv6SocketAddressImplE]+0x50): undefined reference to `Poco::Net::Impl::IPv6SocketAddressImpl::toString[abi:cxx11]() const'
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:94: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:151: recipe for target 'all' failed
make: *** [all] Error 2

据我所知,Poco::Net::IPAddress::IPAddress 必须是 Poco::Net::IPAddress。我不知道为什么链接器会向 Poco::Net::IPAddress 添加额外的 IPAddress 。 这个问题似乎是由 CMakeLists.txt 中的错误引起的 那么我该如何解决这个问题呢?

最佳答案

target_link_libraries(main ${POCO_LIBRARIES}) 将无法按预期工作。该变量仅在查找模块中定义。它甚至可能不适用于现代软件包。

如果你想使用 poco 作为项目子目录,那么我建议链接到他们的目标:

cmake_minimum_required(VERSION 3.4.1)

add_executable(
    main
    main.cpp
)

add_subdirectory(poco)
target_link_libraries(main PUBLIC PocoNet ...) # replace `...` by all poco library you use

关于c++ - 如何在简单的C++项目中使用Poco库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58592243/

相关文章:

cmake - 将GLEW与CMake链接

linux - 使用 CMake 创建二进制文件会删除运行时路径

c++ - 如何开始使用 Poco 库?

c++ - cmake,无法运行与预编译共享库链接的可执行文件

c++ - Poco C++ 库是否支持位置命令行参数?

c++ - 从一对中获取值失败,错误为 : TYPENAME does not provide a call operator

c++ - wxWidgets 显示来自 8 位数组的位图

c++ - 为什么代码不能返回正确的值?

c++ - 有没有办法打印一个对象分配的堆内存量?

c++ - 从 CRAN 安装 pc aPP 包时出现编译时错误,知道这可能是什么原因吗?