Gandalf
Gandalf

Reputation: 3257

How to use Poco library in a simple C++ project?

This is my project's directory structure:

poco/
CMakeLists.txt
main.cpp

note: poco directory contains all the file downloaded from Poco's github repository by using this command:

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

This is the content of the 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})

And this is the content of the main.cpp:

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

    return 0;
}

After I run these commands(in the directory whose structure was presented):

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

I encounter these errors:

[  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

From what I have figured out, Poco::Net::IPAddress::IPAddress must be Poco::Net::IPAddress. And I don't know why the linker adds an extra IPAddress to Poco::Net::IPAddress. And this problem seems to arise from an error in CMakeLists.txt So how can I fix this?

Upvotes: 3

Views: 6704

Answers (3)

Vishal Ahirwar
Vishal Ahirwar

Reputation: 1

It's not a standard way of using cmake but it works

    cmake_minimum_required(VERSION 3.0)
    project(view)
    set(SOURCE ./src/main.cc)#add your additional source file here!
    set(CMAKE_CXX_STANDARD 20)
    set(POCO_LIB_PATH /home/soloknight/vcpkg-master/installed/x64-linux/lib)
    include_directories(/home/soloknight/vcpkg-master/installed/x64-linux/include)
    add_executable(${PROJECT_NAME} ${SOURCE})
    install(TARGETS ${PROJECT_NAME} DESTINATION bin)
    target_link_directories(${PROJECT_NAME} PRIVATE ${POCO_LIB_PATH})
    target_link_libraries(${PROJECT_NAME} PocoFoundation)

I'm using vcpkg to download stuff then linking it to my test project and it's working you have to just set POCO_LIB_PATH and include NOTE : I'm using linux so if you are on windows system maybe you will need extra path setting

Upvotes: 0

maro12
maro12

Reputation: 169

I had a similar question. I did build the following testcase. It is based on a Linux System (opensuse). This example is not perfect -- no guarantee.

bash commands:

$ 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

The bash routines will compile the poco library and installs the needed headers/libraries/cmake/... files locally in ~/libs. Of course this can be changed.


Using the following files.

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();

}

Alternative you can use cmake flags instead of set(CMAKE_PREFIX_PATH ...) in the CMakeLists.txt. This makes it more flexible if you want to use different Poco instances.

Example if you 'installed' Poco to ~/libs:

$ cmake -H. -B./build \
      -DPoco_DIR='/home/<user>/libs/poco/lib/cmake/Poco'

or

$ cmake -H. -B./build \
      -DCMAKE_PREFIX_PATH='/home/<user>/libs/poco/lib/cmake/Poco'

or

$ cmake -H. -B./build \
      -DCMAKE_MODULE_PATH='/home/<user>/libs/poco/lib/cmake/Poco'

Upvotes: 3

Guillaume Racicot
Guillaume Racicot

Reputation: 41770

The line target_link_libraries(main ${POCO_LIBRARIES}) won't work as intended. That variable is only defined in find modules. It may not even work with modern packages.

If you want to use poco as a project subdirectory, then I suggest to link to their targets:

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

Upvotes: 3

Related Questions