无法在Ubuntu上构建HelloWorld Poco示例程序

时间:2019-06-22 14:58:40

标签: c++ ubuntu hyperlink g++ poco-libraries

我刚刚在Ubuntu上安装了Poco(通过从1.9.0版的git master分支上的代码进行编译)。现在,我正在尝试编译HelloWorld.cpp。

这是我尝试过的:

g++  -L/usr/local/lib -lPocoFoundation -lPocoUtil -lPocoNet -lPocoNetd -lPocoData -lPocoXML ./helloworld.cpp

这是helloworld.cpp内容:

#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Util/ServerApplication.h"
#include <iostream>

using namespace Poco;
using namespace Poco::Net;
using namespace Poco::Util;

class HelloRequestHandler: public HTTPRequestHandler
{
    void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
    {
        Application& app = Application::instance();
        app.logger().information("Request from %s", request.clientAddress().toString());

        response.setChunkedTransferEncoding(true);
        response.setContentType("text/html");

        response.send()
            << "<html>"
            << "<head><title>Hello</title></head>"
            << "<body><h1>Hello from the POCO Web Server</h1></body>"
            << "</html>";
    }
};

class HelloRequestHandlerFactory: public HTTPRequestHandlerFactory
{
    HTTPRequestHandler* createRequestHandler(const HTTPServerRequest&)
    {
        return new HelloRequestHandler;
    }
};

class WebServerApp: public ServerApplication
{
    void initialize(Application& self)
    {
        loadConfiguration();
        ServerApplication::initialize(self);
    }

    int main(const std::vector<std::string>&)
    {
        UInt16 port = static_cast<UInt16>(config().getUInt("port", 8080));

        HTTPServer srv(new HelloRequestHandlerFactory, port);
        srv.start();
        logger().information("HTTP Server started on port %hu.", port);
        waitForTerminationRequest();
        logger().information("Stopping HTTP Server...");
        srv.stop();

        return Application::EXIT_OK;
    }
};

POCO_SERVER_MAIN(WebServerApp)

我希望通过继续添加我在g ++命令行中链接的库,最终应该允许我编译程序。

但是似乎无论我尝试链接多少个库,我仍然会遇到以下错误(在消除途中添加了多少个库的情况下并没有消除任何错误):


kennyyu@kennyyu-ubuntu:~/poco/myexample$ g++  -L/usr/local/lib -lPocoFoundation -lPocoUtil -lPocoNet -lPocoNetd -lPocoData -lPocoXML ./helloworld.cpp
/tmp/ccWTd1HS.o: In function `main':
helloworld.cpp:(.text+0x53): undefined reference to `Poco::Util::ServerApplication::run(int, char**)'
helloworld.cpp:(.text+0xd1): undefined reference to `Poco::Exception::displayText[abi:cxx11]() const'
/tmp/ccWTd1HS.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::host() const':
helloworld.cpp:(.text._ZNK4Poco3Net4Impl21IPv4SocketAddressImpl4hostEv[_ZNK4Poco3Net4Impl21IPv4SocketAddressImpl4hostEv]+0x28): undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, unsigned int)'
/tmp/ccWTd1HS.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::host() const':
helloworld.cpp:(.text._ZNK4Poco3Net4Impl21IPv6SocketAddressImpl4hostEv[_ZNK4Poco3Net4Impl21IPv6SocketAddressImpl4hostEv]+0x2e): undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, unsigned int, unsigned int)'
/tmp/ccWTd1HS.o: In function `Poco::ReferenceCounter::ReferenceCounter()':
helloworld.cpp:(.text._ZN4Poco16ReferenceCounterC2Ev[_ZN4Poco16ReferenceCounterC5Ev]+0x19): undefined reference to `Poco::AtomicCounter::AtomicCounter(int)'
/tmp/ccWTd1HS.o: In function `Poco::Logger::log(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Message::Priority)':
helloworld.cpp:(.text._ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE[_ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE]+0xa0): undefined reference to `Poco::Message::Message(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Message::Priority)'
helloworld.cpp:(.text._ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE[_ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE]+0xbe): undefined reference to `Poco::Message::~Message()'
helloworld.cpp:(.text._ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE[_ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE]+0xd2): undefined reference to `Poco::Message::~Message()'
/tmp/ccWTd1HS.o: In function `Poco::Logger::information(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Any const&)':
helloworld.cpp:(.text._ZN4Poco6Logger11informationERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_3AnyE[_ZN4Poco6Logger11informationERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_3AnyE]+0x37): undefined reference to `Poco::format(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Any const&)'
/tmp/ccWTd1HS.o: In function `Poco::Util::Application::logger() const':
helloworld.cpp:(.text._ZNK4Poco4Util11Application6loggerEv[_ZNK4Poco4Util11Application6loggerEv]+0x30): undefined reference to `Poco::Bugcheck::nullPointer(char const*, char const*, int)'
/tmp/ccWTd1HS.o: In function `Poco::Util::Application::instance()':
helloworld.cpp:(.text._ZN4Poco4Util11Application8instanceEv[_ZN4Poco4Util11Application8instanceEv]+0x7): undefined reference to `Poco::Util::Application::_pInstance'
helloworld.cpp:(.text._ZN4Poco4Util11Application8instanceEv[_ZN4Poco4Util11Application8instanceEv]+0x24): undefined reference to `Poco::Bugcheck::nullPointer(char const*, char const*, int)'
helloworld.cpp:(.text._ZN4Poco4Util11Application8instanceEv[_ZN4Poco4Util11Application8instanceEv]+0x2b): undefined reference to `Poco::Util::Application::_pInstance'
/tmp/ccWTd1HS.o: In function `HelloRequestHandler::handleRequest(Poco::Net::HTTPServerRequest&, Poco::Net::HTTPServerResponse&)':
helloworld.cpp:(.text._ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE[_ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE]+0x73): undefined reference to `Poco::Net::SocketAddress::toString[abi:cxx11]() const'
helloworld.cpp:(.text._ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE[_ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE]+0x100): undefined reference to `Poco::Net::HTTPMessage::setChunkedTransferEncoding(bool)'
helloworld.cpp:(.text._ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE[_ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE]+0x139): undefined reference to `Poco::Net::HTTPMessage::setContentType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccWTd1HS.o: In function `HelloRequestHandler::HelloRequestHandler()':
helloworld.cpp:(.text._ZN19HelloRequestHandlerC2Ev[_ZN19HelloRequestHandlerC5Ev]+0x14): undefined reference to `Poco::Net::HTTPRequestHandler::HTTPRequestHandler()'
/tmp/ccWTd1HS.o: In function `WebServerApp::initialize(Poco::Util::Application&)':
helloworld.cpp:(.text._ZN12WebServerApp10initializeERN4Poco4Util11ApplicationE[_ZN12WebServerApp10initializeERN4Poco4Util11ApplicationE]+0x1d): undefined reference to `Poco::Util::Application::loadConfiguration(int)'
helloworld.cpp:(.text._ZN12WebServerApp10initializeERN4Poco4Util11ApplicationE[_ZN12WebServerApp10initializeERN4Poco4Util11ApplicationE]+0x30): undefined reference to `Poco::Util::Application::initialize(Poco::Util::Application&)'
/tmp/ccWTd1HS.o: In function `HelloRequestHandlerFactory::HelloRequestHandlerFactory()':
helloworld.cpp:(.text._ZN26HelloRequestHandlerFactoryC2Ev[_ZN26HelloRequestHandlerFactoryC5Ev]+0x14): undefined reference to `Poco::Net::HTTPRequestHandlerFactory::HTTPRequestHandlerFactory()'
/tmp/ccWTd1HS.o: In function `WebServerApp::main(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)':
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x7c): undefined reference to `Poco::Util::AbstractConfiguration::getUInt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int) const'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0xb6): undefined reference to `Poco::Net::HTTPServerParams::HTTPServerParams()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x11d): undefined reference to `Poco::Net::HTTPServer::HTTPServer(Poco::SharedPtr<Poco::Net::HTTPRequestHandlerFactory, Poco::ReferenceCounter, Poco::ReleasePolicy<Poco::Net::HTTPRequestHandlerFactory> >, unsigned short, Poco::AutoPtr<Poco::Net::HTTPServerParams>)'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x14a): undefined reference to `Poco::Net::TCPServer::start()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x1f6): undefined reference to `Poco::Util::ServerApplication::waitForTerminationRequest()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x273): undefined reference to `Poco::Net::TCPServer::stop()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x287): undefined reference to `Poco::Net::HTTPServer::~HTTPServer()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x3a4): undefined reference to `Poco::Net::HTTPServer::~HTTPServer()'
/tmp/ccWTd1HS.o: In function `WebServerApp::WebServerApp()':
helloworld.cpp:(.text._ZN12WebServerAppC2Ev[_ZN12WebServerAppC5Ev]+0x14): undefined reference to `Poco::Util::ServerApplication::ServerApplication()'
/tmp/ccWTd1HS.o: In function `Poco::ReferenceCounter::~ReferenceCounter()':
helloworld.cpp:(.text._ZN4Poco16ReferenceCounterD2Ev[_ZN4Poco16ReferenceCounterD5Ev]+0x14): undefined reference to `Poco::AtomicCounter::~AtomicCounter()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x20): undefined reference to `Poco::Util::Application::name() const'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x30): undefined reference to `Poco::Util::Application::uninitialize()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x38): undefined reference to `Poco::Util::Application::reinitialize(Poco::Util::Application&)'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x40): undefined reference to `Poco::Util::ServerApplication::defineOptions(Poco::Util::OptionSet&)'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x48): undefined reference to `Poco::Util::ServerApplication::run()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x50): undefined reference to `Poco::Util::Application::handleOption(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccWTd1HS.o: In function `WebServerApp::~WebServerApp()':
helloworld.cpp:(.text._ZN12WebServerAppD2Ev[_ZN12WebServerAppD5Ev]+0x22): undefined reference to `Poco::Util::ServerApplication::~ServerApplication()'
/tmp/ccWTd1HS.o: In function `HelloRequestHandlerFactory::~HelloRequestHandlerFactory()':
helloworld.cpp:(.text._ZN26HelloRequestHandlerFactoryD2Ev[_ZN26HelloRequestHandlerFactoryD5Ev]+0x22): undefined reference to `Poco::Net::HTTPRequestHandlerFactory::~HTTPRequestHandlerFactory()'
/tmp/ccWTd1HS.o: In function `HelloRequestHandler::~HelloRequestHandler()':
helloworld.cpp:(.text._ZN19HelloRequestHandlerD2Ev[_ZN19HelloRequestHandlerD5Ev]+0x22): undefined reference to `Poco::Net::HTTPRequestHandler::~HTTPRequestHandler()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTVN4Poco3Net4Impl21IPv6SocketAddressImplE[_ZTVN4Poco3Net4Impl21IPv6SocketAddressImplE]+0x50): undefined reference to `Poco::Net::Impl::IPv6SocketAddressImpl::toString[abi:cxx11]() const'
/tmp/ccWTd1HS.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::~IPv6SocketAddressImpl()':
helloworld.cpp:(.text._ZN4Poco3Net4Impl21IPv6SocketAddressImplD2Ev[_ZN4Poco3Net4Impl21IPv6SocketAddressImplD5Ev]+0x22): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTVN4Poco3Net4Impl21IPv4SocketAddressImplE[_ZTVN4Poco3Net4Impl21IPv4SocketAddressImplE]+0x50): undefined reference to `Poco::Net::Impl::IPv4SocketAddressImpl::toString[abi:cxx11]() const'
/tmp/ccWTd1HS.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::~IPv4SocketAddressImpl()':
helloworld.cpp:(.text._ZN4Poco3Net4Impl21IPv4SocketAddressImplD2Ev[_ZN4Poco3Net4Impl21IPv4SocketAddressImplD5Ev]+0x22): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTI12WebServerApp[_ZTI12WebServerApp]+0x10): undefined reference to `typeinfo for Poco::Util::ServerApplication'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTI26HelloRequestHandlerFactory[_ZTI26HelloRequestHandlerFactory]+0x10): undefined reference to `typeinfo for Poco::Net::HTTPRequestHandlerFactory'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTI19HelloRequestHandler[_ZTI19HelloRequestHandler]+0x10): undefined reference to `typeinfo for Poco::Net::HTTPRequestHandler'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
/tmp/ccWTd1HS.o:(.data.rel.local.DW.ref._ZTIN4Poco9ExceptionE[DW.ref._ZTIN4Poco9ExceptionE]+0x0): undefined reference to `typeinfo for Poco::Exception'
collect2: error: ld returned 1 exit status
kennyyu@kennyyu-ubuntu:~/poco/myexample$

kennyyu@kennyyu-ubuntu:~/poco/myexample$ ls /usr/local/lib/libPoco*.so
/usr/local/lib/libPocoCppParserd.so  /usr/local/lib/libPocoDataMySQL.so  /usr/local/lib/libPocoDataSQLited.so  /usr/local/lib/libPocoFoundationd.so  /usr/local/lib/libPocoMongoDBd.so  /usr/local/lib/libPocoPDFd.so    /usr/local/lib/libPocoUtild.so  /usr/local/lib/libPocoZipd.so
/usr/local/lib/libPocoCppParser.so   /usr/local/lib/libPocoDataODBCd.so  /usr/local/lib/libPocoDataSQLite.so   /usr/local/lib/libPocoFoundation.so   /usr/local/lib/libPocoMongoDB.so   /usr/local/lib/libPocoPDF.so     /usr/local/lib/libPocoUtil.so   /usr/local/lib/libPocoZip.so
/usr/local/lib/libPocoDatad.so       /usr/local/lib/libPocoDataODBC.so   /usr/local/lib/libPocoEncodingsd.so   /usr/local/lib/libPocoJSONd.so        /usr/local/lib/libPocoNetd.so      /usr/local/lib/libPocoRedisd.so  /usr/local/lib/libPocoXMLd.so
/usr/local/lib/libPocoDataMySQLd.so  /usr/local/lib/libPocoData.so       /usr/local/lib/libPocoEncodings.so    /usr/local/lib/libPocoJSON.so         /usr/local/lib/libPocoNet.so       /usr/local/lib/libPocoRedis.so   /usr/local/lib/libPocoXML.so
kennyyu@kennyyu-ubuntu:~/poco/myexample$

有人可以给我一些启示吗?

2 个答案:

答案 0 :(得分:0)

尽管您尝试与Poco库链接,但链接器似乎无法找到所有这些库。检查您是否已构建 libPocoNet.so libPocoUtil.so ,它们是否在库路径中。

答案 1 :(得分:0)

这在Ubuntu 19.10中对我有用。

sudo apt install libpoco-dev

(所有Poco库均已安装。)

使用OP中的内容创建helloworld.cpp。

编译为:

g++ helloworld.cpp -o helloworld.o -lPocoFoundation -lPocoNet -lPocoUtil

如果仍然不起作用,则可以在Poco .h文件中带有-I选项的位置以及Poco .so文件中带有-L选项的位置。

找到Poco .so文件的位置:

sudo find / -name "libPoco*.so" 2>/dev/null
/usr/lib/x86_64-linux-gnu/libPocoFoundation.so
...

sudo find / -name "ServerSocket.h" 2>/dev/null
/usr/include/Poco/Net/ServerSocket.h

然后,编译语句变为:

g++ -I/usr/include/Poco/ helloworld.cpp -o helloworld.o -L/usr/lib/x86_64-linux-gnu/ -lPocoFoundation -lPocoNet -lPocoUtil