Undefined symbols for architecture x86_64 (curlpp)
在我正在研究的项目中,我试图使用curlpp库发出一个简单的html GET请求。我使用以下选项将cpp文件传递给clang:
1 | clang++ -std=c++11 -stdlib=libc++ -I /usr/local/Cellar url_test.cpp |
然后我得到这些错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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还是很陌生,所以将不胜感激!
您必须告诉编译器哪些库与可执行文件链接。对于您而言,此问题可通过添加选项
来解决。
顺便说一句,您提供的路径不正确,因为标头实际上位于/ usr / local / Cellar / curlpp / [在此处插入版本] / include。无论如何,由于它会自动在/ usr / local / include中创建符号链接,这是编译器搜索的默认位置之一,因此无需为通过自制程序安装的库添加包含搜索路径就可以很好地进行编译。
您的问题是您没有链接使用的库。添加库,您应该会很好。