2

I'm currently trying to develop a C++ cross-platform project in VSCode which should include the Poco library which I downloaded using vcpkg. I then followed this tutorial: https://www.codeproject.com/Articles/252566/Learning-Poco-GET-with-HTTP but I cannot resolve the undefined references errors that also the article shows. Here is my main (httpGet.cpp) (which I copied from the link above):

#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/StreamCopier.h>
#include <Poco/Path.h>
#include <Poco/URI.h>
#include <Poco/Exception.h>
#include <iostream>
#include <string>

using namespace Poco::Net;
using namespace Poco;
using namespace std;

int main(int argc, char **argv)
{
  if (argc != 2)
  {
    cout << "Usage: " << argv[0] << " <uri>" << endl;
    cout << "       fetches the resource identified by <uri> and print it" << endl;
    return -1;
  }

  try
  {
    // prepare session
    URI uri(argv[1]);
    HTTPClientSession session(uri.getHost(), uri.getPort());

    // prepare path
    string path(uri.getPathAndQuery());
    if (path.empty()) path = "/";

    // send request
    HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
    session.sendRequest(req);

    // get response
    HTTPResponse res;
    cout << res.getStatus() << " " << res.getReason() << endl;

    // print response
    istream &is = session.receiveResponse(res);
    StreamCopier::copyStream(is, cout);
  }
  catch (Exception &ex)
  {
    cerr << ex.displayText() << endl;
    return -1;
  }

  return 0;
}

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(PocoNet_DIR "C:/vcpkg/installed/x64-windows/share/poco")
set(PocoFoundation_DIR "C:/vcpkg/installed/x64-windows/share/poco")

set(VCPKG_TARGET_TRIPLET "x64-windows"
      CACHE STRING "")
set(CMAKE_TOOLCHAIN_FILE "C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
      CACHE STRING "")
project(TryPOCO)

find_package(Poco CONFIG REQUIRED Net)
find_package(PocoNet REQUIRED)

include_directories("C:/vcpkg/installed/x64-windows/include")
link_directories("C:/vcpkg/installed/x64-windows/lib")

add_executable(${PROJECT_NAME} httpGet.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE PocoNet)

And here is the error I get:

[build] CMakeFiles\TryPOCO.dir/objects.a(httpGet.cpp.obj): In function `main':
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:27: undefined reference to `Poco::URI::URI(char const*)'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:28: undefined reference to `Poco::URI::getPort() const'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:28: undefined reference to `Poco::Net::HTTPClientSession::HTTPClientSession(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned short)'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:31: undefined reference to `Poco::URI::getPathAndQuery[abi:cxx11]() const'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:35: undefined reference to `Poco::Net::HTTPRequest::HTTPRequest(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&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:36: undefined reference to `Poco::Net::HTTPClientSession::sendRequest(Poco::Net::HTTPRequest&)'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:39: undefined reference to `Poco::Net::HTTPResponse::HTTPResponse()'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:43: undefined reference to `Poco::Net::HTTPClientSession::receiveResponse(Poco::Net::HTTPResponse&)'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:44: undefined reference to `Poco::StreamCopier::copyStream(std::istream&, std::ostream&, unsigned long long)'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:39: undefined reference to `Poco::Net::HTTPResponse::~HTTPResponse()'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:35: undefined reference to `Poco::Net::HTTPRequest::~HTTPRequest()'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:28: undefined reference to `Poco::Net::HTTPClientSession::~HTTPClientSession()'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:27: undefined reference to `Poco::URI::~URI()'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:39: undefined reference to `Poco::Net::HTTPResponse::~HTTPResponse()'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:35: undefined reference to `Poco::Net::HTTPRequest::~HTTPRequest()'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:28: undefined reference to `Poco::Net::HTTPClientSession::~HTTPClientSession()'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:27: undefined reference to `Poco::URI::~URI()'
[build] C:/TesiMag/ProveVSCode/TryPOCO/httpGet.cpp:48: undefined reference to `Poco::Exception::displayText[abi:cxx11]() const'
[build] CMakeFiles\TryPOCO.dir/objects.a(httpGet.cpp.obj): In function `Poco::Net::Impl::IPv4SocketAddressImpl::host() const':
[build] C:/vcpkg/installed/x64-windows/include/Poco/Net/SocketAddressImpl.h:81: undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, int)'
[build] CMakeFiles\TryPOCO.dir/objects.a(httpGet.cpp.obj): In function `Poco::Net::Impl::IPv6SocketAddressImpl::host() const':
[build] C:/vcpkg/installed/x64-windows/include/Poco/Net/SocketAddressImpl.h:143: undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, int, unsigned int)'
[build] CMakeFiles\TryPOCO.dir/objects.a(httpGet.cpp.obj):httpGet.cpp:(.rdata$_ZTVN4Poco3Net4Impl21IPv6SocketAddressImplE[_ZTVN4Poco3Net4Impl21IPv6SocketAddressImplE]+0x50): undefined reference to `Poco::Net::Impl::IPv6SocketAddressImpl::toString[abi:cxx11]() const'
[build] CMakeFiles\TryPOCO.dir/objects.a(httpGet.cpp.obj): In function `Poco::Net::Impl::IPv6SocketAddressImpl::~IPv6SocketAddressImpl()':
[build] C:/vcpkg/installed/x64-windows/include/Poco/Net/SocketAddressImpl.h:118: undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
[build] CMakeFiles\TryPOCO.dir/objects.a(httpGet.cpp.obj):httpGet.cpp:(.rdata$_ZTVN4Poco3Net4Impl21IPv4SocketAddressImplE[_ZTVN4Poco3Net4Impl21IPv4SocketAddressImplE]+0x50): undefined reference to `Poco::Net::Impl::IPv4SocketAddressImpl::toString[abi:cxx11]() const'
[build] CMakeFiles\TryPOCO.dir/objects.a(httpGet.cpp.obj): In function `Poco::Net::Impl::IPv4SocketAddressImpl::~IPv4SocketAddressImpl()':
[build] C:/vcpkg/installed/x64-windows/include/Poco/Net/SocketAddressImpl.h:56: undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
[build] CMakeFiles\TryPOCO.dir/objects.a(httpGet.cpp.obj):httpGet.cpp:(.rdata$.refptr._ZN4Poco3Net11HTTPRequest8HTTP_GETB5cxx11E[.refptr._ZN4Poco3Net11HTTPRequest8HTTP_GETB5cxx11E]+0x0): undefined reference to `Poco::Net::HTTPRequest::HTTP_GET[abi:cxx11]'
[build] CMakeFiles\TryPOCO.dir/objects.a(httpGet.cpp.obj):httpGet.cpp:(.rdata$.refptr._ZN4Poco3Net11HTTPMessage8HTTP_1_1B5cxx11E[.refptr._ZN4Poco3Net11HTTPMessage8HTTP_1_1B5cxx11E]+0x0): undefined reference to `Poco::Net::HTTPMessage::HTTP_1_1[abi:cxx11]'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\TryPOCO.dir\build.make:86: TryPOCO.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:75: CMakeFiles/TryPOCO.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:83: all] Error 2
[build] Compilazione terminata con codice di uscita 2

The compiler I am using is g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0.

I suppose it's a linker error, but I am new to CMake so I don't really know if there are any other libraries that I should add to target_link_libraries or somewhere else in the CMakeLists.txt. Also I don't know if the CMakeLists.txt is written in a good manner since it's the first time ever I use CMake.

Thank you in advance for your answers.

slack off
  • 21
  • 2
  • Welcome to Stack Overflow! It looks like `find_package(Poco ...)`, if successful, should provide some imported targets you can use in `target_link_libraries()`. There is already an example in [this](https://stackoverflow.com/a/36134007/3987854) answer. – Kevin Mar 22 '20 at 17:37
  • Hi, thank you for your answer! I tried to modify the **CMakeLists.txt** as the link you provided suggest. In my **CMakeLists.txt** I changed `find_package(Poco CONFIG REQUIRED Net)` to `find_package(Poco CONFIG REQUIRED Foundation Net Util)` and `target_link_libraries(${PROJECT_NAME} PRIVATE PocoNet)` to `target_link_libraries(${PROJECT_NAME} PRIVATE Poco::Foundation Poco::Util Poco::Net)` but the error I get is exactly the same – slack off Mar 23 '20 at 11:25
  • Did you delete your CMake cache (`CMakeCache.txt`) file, and run CMake from scratch? – Kevin Mar 23 '20 at 11:47
  • I'm using the CMake extension for VSCode (CMake and CMake Tools): First I run command `CMake: Configure`, then `CMake: Build` and if I change the **CMakeLists.txt** I run `CMake: Clean` before running `CMake: Configure` again. I also tried to delete the **CMakeCache.txt** but I continue getting the same error – slack off Mar 23 '20 at 12:55
  • Ok, that should be sufficient to remove the CMake cache, just wanted to be sure! – Kevin Mar 23 '20 at 12:56
  • The problem is that also removing the cache the error remains – slack off Mar 24 '20 at 10:08

1 Answers1

0

I think you need a find_library in CMakeLists.txt.

See https://cmake.org/cmake/help/cmake2.6docs.html#command:find_library

Okkenator
  • 1,654
  • 1
  • 15
  • 27
  • I tried to add `find_library(PocoNet REQUIRED)` but unfortunately the error remains – slack off Mar 24 '20 at 10:06
  • You will have to check what the library file name is that is generated by `PocoNet`. It sounds like your `CMake` files are not set up correctly. Maybe you want to go through the Tutorial https://cmake.org/cmake/help/latest/guide/tutorial/index.html and apply it to your project. – Okkenator Mar 24 '20 at 13:37
  • I followed the tutorial as you suggested, I have a file named `PocoNet.lib` and a `PocoNet.dll` and tried to use them in my **CMakeLists.txt**, but I get the same error. Also, if I try to compile not using CMake but giving the compiler the location of headers and _.lib_ files I get the same identical error that CMake returns. – slack off Mar 25 '20 at 12:48
  • I am assuming your link command includes `-I dirname` to tell it to look in dirname for libraries, but does it also include the `-lPocoNet` parameter? – Okkenator Mar 25 '20 at 14:07
  • It includes `-I dirname` for the includes and `-L dirname` for the _.lib_ file then `-lPocoNet` but it seems to not having any effect because the undefined reference are still there – slack off Mar 25 '20 at 16:04