【问题标题】:Cmake undefined references when using the external library Poco (installed via vcpkg) in Visual Studio Code在 Visual Studio Code 中使用外部库 Poco(通过 vcpkg 安装)时的 Cmake 未定义引用
【发布时间】:2020-07-03 06:23:07
【问题描述】:

我目前正在尝试在 VSCode 中开发一个 C++ 跨平台项目,其中应该包括我使用 vcpkg 下载的 Poco 库。然后我按照本教程进行操作:https://www.codeproject.com/Articles/252566/Learning-Poco-GET-with-HTTP 但我无法解决文章也显示的未定义引用错误。 这是我的 ma​​in (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-rev0, Built by MinGW-W64 project) 8.1.0。

我想这是一个链接器错误,但我是 CMake 的新手,所以我真的不知道是否应该将任何其他库添加到 target_link_libraries 或 中的其他位置CMakeLists.txt。 另外我不知道 CMakeLists.txt 是否写得很好,因为这是我第一次使用 CMake。

提前感谢您的回答。

【问题讨论】:

  • 欢迎来到 Stack Overflow!看起来find_package(Poco ...),如果成功,应该提供一些可以在target_link_libraries() 中使用的导入目标。 this答案中已经有例子了。
  • 您好,谢谢您的回答!我尝试按照您提供的链接建议修改 CMakeLists.txt。在我的 CMakeLists.txt 中,我将 find_package(Poco CONFIG REQUIRED Net) 更改为 find_package(Poco CONFIG REQUIRED Foundation Net Util) 并将 target_link_libraries(${PROJECT_NAME} PRIVATE PocoNet) 更改为 target_link_libraries(${PROJECT_NAME} PRIVATE Poco::Foundation Poco::Util Poco::Net) 但我得到的错误完全相同
  • 您是否删除了您的 CMake 缓存 (CMakeCache.txt) 文件,然后从头开始运行 CMake?
  • 我正在使用 VSCode 的 CMake 扩展(CMake 和 CMake 工具):首先我运行命令 CMake: Configure,然后运行 ​​CMake: Build,如果我更改了 CMakeLists.txt 我运行CMake: Clean,然后再次运行CMake: Configure。我也尝试删除 CMakeCache.txt 但我继续收到相同的错误
  • 好的,这应该足以删除 CMake 缓存,只是想确定一下!

标签: visual-studio-code cmake linker-errors poco-libraries vcpkg


【解决方案1】:

我认为您需要 CMakeLists.txt 中的 find_library。

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

【讨论】:

  • 我尝试添加find_library(PocoNet REQUIRED),但不幸的是错误仍然存​​在
  • 您必须检查PocoNet 生成的库文件名是什么。听起来您的 CMake 文件设置不正确。也许您想通过教程cmake.org/cmake/help/latest/guide/tutorial/index.html 并将其应用到您的项目中。
  • 我按照您的建议按照教程进行操作,我有一个名为 PocoNet.lib 和 PocoNet.dll 的文件,并尝试在我的 CMakeLists.txt 中使用它们,但我得到了同样的错误。此外,如果我尝试不使用 CMake 进行编译,而是为编译器提供头文件和 .lib 文件的位置,我会得到与 CMake 返回相同的错误。
  • 我假设您的链接命令包含-I dirname 告诉它在目录名中查找库,但它是否还包含-lPocoNet 参数?
  • 它包括 -I dirname 用于包含和 -L dirname 用于 .lib 文件然后 -lPocoNet 但它似乎没有任何效果,因为未定义的引用仍然存在那里
猜你喜欢
  • 2020-12-30
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
  • 1970-01-01
  • 2019-08-25
  • 2020-07-18
  • 1970-01-01
  • 2019-04-05
相关资源
最近更新 更多