这是我的项目的目录结构:
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
中增加了一个额外的Poco::Net::IPAddress
。这个问题似乎是由CMakeLists.txt中的一个错误引起的,那么我如何解决这个问题呢?
发布于 2021-09-04 09:44:02
我也有类似的问题。我确实构建了下面的测试案例。它基于Linux系统(opensuse)。这个例子并不完美--不能保证。
bash
命令:
$ git clone --depth=1 --branch=master --recurse-submodules https://github.com/pocoproject/poco.git
$ cd poco && mkdir poco-build && cd poco-build
$ cmake -H .. -B . -DCMAKE_INSTALL_PREFIX=~/libs
$ cmake --build . --target install
bash例程将编译poco库并安装所需的头/库/cmake/.~/libs
中的本地文件。当然这是可以改变的。
使用以下文件。
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(PocoTimer)
set(CMAKE_PREFIX_PATH /home/<user>/libs/poco/lib/cmake/Poco)
find_package(Poco REQUIRED Foundation)
add_executable(
main main.cpp
)
target_link_libraries(
main
PRIVATE Poco::Foundation
)
main.cpp
#include "Poco/Timer.h"
#include "Poco/Thread.h"
#include "Poco/Stopwatch.h"
#include <iostream>
/*
* Main application
*/
class PocoTimer
{
public:
//------------
PocoTimer() { stopWatch.start(); }
//------------
void PrintElapsedTime(Poco::Timer& timer)
{
std::cout << "Time elapsed: " << stopWatch.elapsed() / 1000 << " milliseconds." << std::endl;
}
private:
Poco::Stopwatch stopWatch;
};
int main() {
PocoTimer pocoTimer;
Poco::Timer timer(250, 500);
// -- print command -- //
timer.start(Poco::TimerCallback<PocoTimer>(pocoTimer, &PocoTimer::PrintElapsedTime));
Poco::Thread::sleep(5000);
timer.stop();
}
另外,您可以在cmake
中使用set(CMAKE_PREFIX_PATH ...)
标志而不是set(CMAKE_PREFIX_PATH ...)
标志。如果您想要使用不同的Poco实例,这将使它更加灵活。
示例如果您将Poco“安装”到~/libs
$ cmake -H. -B./build \
-DPoco_DIR='/home/<user>/libs/poco/lib/cmake/Poco'
或
$ cmake -H. -B./build \
-DCMAKE_PREFIX_PATH='/home/<user>/libs/poco/lib/cmake/Poco'
或
$ cmake -H. -B./build \
-DCMAKE_MODULE_PATH='/home/<user>/libs/poco/lib/cmake/Poco'
https://stackoverflow.com/questions/58592243
复制相似问题