cmake 在Visual Studio Code中使用外部库Poco(通过vcpkg安装)时,生成未定义的引用

x7yiwoj4  于 2023-10-20  发布在  其他
关注(0)|答案(2)|浏览(165)

我目前正在尝试在VSCode中开发一个C++跨平台项目,其中应该包括我使用vcpkg下载的Poco库。然后我遵循了这个教程:https://www.codeproject.com/Articles/252566/Learning-Poco-GET-with-HTTP但我不能解决文章中显示的未定义引用错误。下面是我的main(httpGet.cpp)(我从上面的链接复制):

#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;
}

下面是我的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)

下面是我得到的错误:

[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

我使用的编译器是g++(x86_64-posix-seh-rev 0,Built by MinGW-W 64 project)8.1.0
我想这是一个链接器错误,但我是CMake的新手,所以我真的不知道是否有任何其他库,我应该添加到 target_link_libraries 或其他地方在CMakeLists.txt。我也不知道CMakeLists.txt是否写得很好,因为这是我第一次使用CMake。
提前感谢您的回答。

enyaitl3

enyaitl31#

我认为你需要在CMakeLists.txt中找到一个find_library。
请访问https://cmake.org/cmake/help/cmake2.6docs.html#command:find_library

qlckcl4x

qlckcl4x2#

给予答案太晚了,不过好吧。我注意到,当尝试使用其他与vcpkg一起安装的库时,会出现类似的错误(我在fmt中也遇到过类似的错误)。另外,当我尝试使用另一个编译器时(我将编译器从mingw更改为msvc),这个错误消失了。我希望这些观察对某人有用)

相关问题