在我正在开发的项目中,我正在尝试使用 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很新,所以任何帮助都会非常感激!
答案 0 :(得分:1)
您必须告诉编译器哪些库与您的可执行文件链接。在您的情况下,通过添加选项-lcurlpp -lcurl
来解决问题。
顺便说一句,您提供的路径不正确,因为标题实际上位于 / usr / local / Cellar / curlpp / [在此处插入版本] / include 。无论如何,这可以很好地编译,而无需为通过自制软件安装的库添加包含搜索路径,因为它会自动在 / usr / local / include 中创建符号链接,这是编译器搜索的默认位置之一。
答案 1 :(得分:0)
您的问题是您没有链接您使用的库。添加库,你应该很好。