我编写了一个c++程序,并尝试使用curlpp通过SSL安全地访问http服务器。问题是,我不能连接程序。
操作系统:Ubuntu15.10
错误:
/usr/bin/cmake -E cmake_link_script CMakeFiles/prtg_probe.dir/ Wird .std *选项容器( /usr/include/curlpp/internal/OptionContainer.inl:38:,std::allocator >,std::allocator,std::allocator >>> const&)':/usr/include/curlpp/internal/OptionContainer.inl:38:未定义的引用curlpp::internal::SList::SList(std::__cxx11::list,std::allocator >,std::>>> const&)‘CMakeFiles/prtg_probe.dir/probe.cpp.o:在函数curlpp中:内部:选项容器,std::分配程序>,std::allocator,std::allocator >>>:setValue(std::__cxx11::list,std::allocator >,std::allocator,>>>const&):/usr/include/curlpp/internal/OptionContainer.inl:52:未定义引用`curlpp::internal::SList::operator=(std::__cxx11::list,std::`curlpp::internal::SList::operator=(std::__cxx11::list,>,std::allocator,std::allocator >>>const&).
这是生成错误的代码:
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <cctype>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <cerrno>
#include <boost/bind.hpp>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
....
class myclass
{
....
try
{
curlpp::Cleanup cleaner;
curlpp::Easy request;
ostringstream os;
request.setOpt(new curlpp::options::Url(&url_announce[0]));
request.setOpt(new curlpp::options::SslEngineDefault());
list<string> header;
header.push_back("Content-Type: text/*");
request.setOpt(new curlpp::options::HttpHeader(header));
request.setOpt(new curlpp::options::PostFields(data_announce));
request.setOpt(new curlpp::options::PostFieldSize((long)data_announce.length()));
request.setOpt(new curlpp::options::WriteStream(&os));
request.perform();
request_announce = os.str();
}
catch (curlpp::LogicError & e)
{
syslog(LOG_DAEMON, "Error accessing PRTG server: %s", e.what());
}
catch (curlpp::RuntimeError & e)
{
syslog(LOG_DAEMON, "Error accessing PRTG server: %s", e.what());
}使用curlpp::options::...的每一行在链接时都会产生一个错误。我环顾四周搜索了几个小时,但我发现的只是把libcurl和libcurl链接在一起。我这么做了,但还是会犯这个错误。
以下是完整的链接线:
/usr/bin/c++ -g CMakeFiles/prtg_probe.dir/sensors.cpp.o
CMakeFiles/prtg_probe.dir/mini_probe.cpp.o CMakeFiles/prtg_probe.dir/probe.cpp.o
CMakeFiles/prtg_probe.dir/helper.cpp.o
CMakeFiles/prtg_probe.dir/config.cpp.o CMakeFiles/prtg_probe.dir/main.cpp.o
-o prtg_probe -rdynamic -lm -lpthread -lcrypto -lssl -lcurlpp -lcurl -lboost_system -lboost_filesystem -ljsoncpp -luuid有人知道我错过了什么吗?我需要再加一个图书馆吗?如果有,哪一个?
我尝试用以下命令编译example00.cpp:
g++ -o example00 example00.cpp -lm -lcurl -lcurlpp其结果是:
/tmp/cc3pcvDc.o: In Funktion `curlpp::OptionTrait<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, (CURLoption)10002>::updateHandleToMe(curlpp::internal::CurlHandle*) const':
example00.cpp: (.text._ZNK6curlpp11OptionTraitINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEL10CURLoption10002EE16updateHandleToMeEPNS_8internal10CurlHandleE[_ZNK6curlpp11OptionTraitINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEL10CURLoption10002EE16updateHandleToMeEPNS_8internal10CurlHandleE]+0x68):
Nicht definierter Verweis auf `curlpp::UnsetOption::UnsetOption(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cc3pcvDc.o: In Funktion `curlpp::Option<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::getValue() const':
example00.cpp: (.text._ZNK6curlpp6OptionINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE8getValueEv[_ZNK6curlpp6OptionINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE8getValueEv]+0x68):
Nicht definierter Verweis auf `curlpp::UnsetOption::UnsetOption(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status在我看来这也是同样的问题。
发布于 2015-11-03 18:18:59
问题是ccurlcpp::UnsetOption::UnsetOption的连接在lipcurlcpp.so二进制文件中有部分缺陷。
该链接商的投诉:
g++ -o example00 example00.cpp -lm -lcurl -lcurlpp是:
undefined reference to `curlpp::UnsetOption::UnsetOption(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'但是如果我在libcurlpp.so中分解构造器签名
nm -D -C libcurlpp.so | grep UnsetOption::UnsetOption我看到:
0000000000021776 T curlpp::UnsetOption::UnsetOption(char const*)
000000000002173e T curlpp::UnsetOption::UnsetOption(std::string const&)由于某些原因,std::string一直没有被正确地删除。如果我从curlpp 0.7.3源代码包Exception.cpp中获得定义此构造函数的源文件,请编译它:
curlpp-0.7.3/src/curlpp$ g++ -I../../include -I. -c Exception.cpp然后从对象文件中解析构造器签名:
nm -C Exception.o | grep UnsetOption::UnsetOption我得到:
00000000000003f4 T curlpp::UnsetOption::UnsetOption(char const*)
00000000000003c2 T curlpp::UnsetOption::UnsetOption(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)所以:
curlpp::UnsetOption::UnsetOption(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)是编译器告诉链接器要查找的签名,但这不是库中的签名。对错误的简短解释是:库被破坏了。
但是,我们发现这种不一致不会影响构造函数的其他重载:
curlpp::UnsetOption::UnsetOption(char const*)它也不能,因为char const *是一个内置类型。
这样就可以进行黑客修复。编译未定义引用调用的文件是(已安装的) /usr/include/curlpp/Option.inl,位于以下行:
throw UnsetOption(std::string("You are trying to set an unset option to a handle"));以root用户身份编辑此文件,您将看到它(不一致)包含以下两个实例:
throw UnsetOption(std::string("blah blah"));其中一个例子是:
throw UnsetOption("blah blah");将UnsetOption(std::string("blah blah"))出现的次数更改为UnsetOption("blah blah")。然后在这个文件中只调用好的构造函数,至少example00将编译和链接。
如果您不喜欢黑客,或者发现问题再次出现在其他地方,那么您可以下载ubuntu源代码包curlpp_0.7.3.orig.tar.gz并自己构建和安装它。这是正确的补救办法。
发布于 2017-06-01 11:47:55
您可以尝试使用旧的ABI编译您的项目:
g++ -o example00 example.00.cpp -D_GLIBCXX_USE_CXX11_ABI=0 -lm -lcurl -lcurlpp
发布于 2019-10-30 09:35:02
对我来说,以下是Ubuntu18.04的工作:
apt-get install libcurl4-openssl-dev
apt-get install libcurlpp-devCmake:
...
add_executable(HTTPPostTest HTTPPostTest.cpp)
TARGET_LINK_LIBRARIES(HTTPPostTest -lcurl -lcurlpp stdc++fs )
...HTTPPostTest.cpp:
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
int main(int argc, char *argv[]) {
try {
curlpp::Cleanup cleaner;
curlpp::Easy request;
request.setOpt(new curlpp::options::Url("https://postman-echo.com/post"));
request.setOpt(new curlpp::options::Verbose(true));
std::list<std::string> header;
header.push_back("Content-Type: application/octet-stream");
request.setOpt(new curlpp::options::HttpHeader(header));
request.setOpt(new curlpp::options::PostFields("abcd"));
request.setOpt(new curlpp::options::PostFieldSize(5));
request.perform();
} catch ( curlpp::LogicError & e ) {
std::cout << e.what() << std::endl;
} catch ( curlpp::RuntimeError & e ) {
std::cout << e.what() << std::endl;
}
return EXIT_SUCCESS;
}https://stackoverflow.com/questions/33420631
复制相似问题