1

在我正在处理的项目中,我正在尝试使用curlpp库来发出简单的 html GET 请求。我使用以下选项将 cpp 文件传递​​给 clang++:

clang++ -std=c++11 -stdlib=libc++ -I /usr/local/Cellar url_test.cpp

然后我得到这些错误:

Undefined symbols for architecture x86_64:
  "curlpp::OptionBase::OptionBase(CURLoption)", referenced from:
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Option(CURLoption, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in url_test-541b93.o
  "curlpp::OptionBase::~OptionBase()", referenced from:
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Option(CURLoption, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in url_test-541b93.o
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::~Option() in url_test-541b93.o
  "curlpp::UnsetOption::UnsetOption(char const*)", referenced from:
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::updateMeToOption(curlpp::OptionBase const&) in url_test-541b93.o
      curlpp::OptionTrait<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, (CURLoption)10002>::updateHandleToMe(curlpp::internal::CurlHandle*) const in url_test-541b93.o
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::getValue() const in url_test-541b93.o
  "curlpp::RuntimeError::~RuntimeError()", referenced from:
      curlpp::UnsetOption::~UnsetOption() in url_test-541b93.o
  "curlpp::libcurlRuntimeAssert(char const*, CURLcode)", referenced from:
      void curlpp::internal::CurlHandle::option<void*>(CURLoption, void*) in url_test-541b93.o
  "curlpp::Easy::perform()", referenced from:
      _main in url_test-541b93.o
  "curlpp::Easy::Easy()", referenced from:
      _main in url_test-541b93.o
  "curlpp::Easy::~Easy()", referenced from:
      _main in url_test-541b93.o
  "curlpp::Cleanup::Cleanup()", referenced from:
      _main in url_test-541b93.o
  "curlpp::Cleanup::~Cleanup()", referenced from:
      _main in url_test-541b93.o
  "curlpp::OptionBase::operator<(curlpp::OptionBase const&) const", referenced from:
      vtable for curlpp::OptionTrait<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, (CURLoption)10002> in url_test-541b93.o
      vtable for curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > in url_test-541b93.o
  "typeinfo for curlpp::LogicError", referenced from:
      GCC_except_table0 in url_test-541b93.o
  "typeinfo for curlpp::OptionBase", referenced from:
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::updateMeToOption(curlpp::OptionBase const&) in url_test-541b93.o
      typeinfo for curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > in url_test-541b93.o
  "typeinfo for curlpp::RuntimeError", referenced from:
      GCC_except_table0 in url_test-541b93.o
      typeinfo for curlpp::UnsetOption in url_test-541b93.o
  "_curl_easy_setopt", referenced from:
      void curlpp::internal::CurlHandle::option<void*>(CURLoption, void*) in url_test-541b93.o
ld: symbol(s) not found for architecture x86_64

我认为这意味着编译器找不到任何 curlpp 库。

这是我要运行的代码:

  1 #include <string>
  2 #include <sstream>
  3 #include <iostream>
  4 #include <curlpp/cURLpp.hpp>
  5 #include <curlpp/Easy.hpp>
  6 #include <curlpp/Options.hpp>
  7 #include <fstream>
  8 
  9 using namespace curlpp::options;
 10 
 11 int main(int, char **)
 12 {
 13     try
 14     {
 15         curlpp::Cleanup testCleanup;
 16         curlpp::Easy miRequest;
 17         miRequest.setOpt<Url>("http://www.wikipedia.org");
 18         miRequest.perform();
 19     }
 20     catch(curlpp::RuntimeError & e)
 21     {
 22         std::cout << e.what() << std::endl;
 23     }
 24     catch(curlpp::LogicError & e)
 25     {
 26         std::cout << e.what() << std::endl;
 27     }
 28 
 29     return 0;
 30 }

我正在运行安装了 Xcode 命令行工具和自制软件的 macOS Sierra 10.12.4。我自制了 curlpp 库。我知道其他人能够使用 gcc 在 Ubuntu 16.04 上编译这个项目,所以我认为问题与 macOS 有关。

我对 cpp 很陌生,所以任何帮助将不胜感激!

4

2 回答 2

1

您必须告诉编译器哪些库与您的可执行文件链接。在您的情况下,问题是通过添加 options 来解决的-lcurlpp -lcurl

顺便说一句,您提供的路径不正确,因为标头实际上位于/usr/local/Cellar/curlpp/[insert version here]/include中。无论如何,这编译得很好,无需为通过自制软件安装的库添加包含搜索路径,因为它会自动在/usr/local/include中创建符号链接,这是编译器搜索的默认位置之一。

于 2017-06-16T07:56:28.883 回答
0

您的问题是您没有链接您使用的库。添加图书馆,你应该很好。

于 2017-06-16T06:17:09.557 回答