简体   繁体   English

curlpp 链接错误

[英]Link errors with curlpp

I wrote a c++ program and try to use curlpp to access a http server securely over SSL .我编写了一个 c++ 程序并尝试使用 curlpp 通过SSL安全地访问 http 服务器。 The problem is, that I can't link the program.问题是,我无法链接该程序。

Operating system: Ubuntu 15.10操作系统: Ubuntu 15.10

Errors:错误:

/usr/bin/cmake -E cmake_link_script CMakeFiles/prtg_probe.dir/link.txt --verbose=1 Wird gelinkt prtg_probe (c++) CMakeFiles/prtg_probe.dir/probe.cpp.o: In function curlpp::internal::OptionContainer, std::allocator >, std::allocator, std::allocator > > > /usr/bin/cmake -E cmake_link_script CMakeFiles/prtg_probe.dir/link.txt --verbose=1 Wird gellinkt prtg_probe (c++) CMakeFiles/prtg_probe.dir/probe.cpp.o: 在函数 curlpp::internal::OptionContainer , std::allocator >, std::allocator, std::allocator > > >

::OptionContainer(std::__cxx11::list, std::allocator >, std::allocator, std::allocator > > > const&)': /usr/include/curlpp/internal/OptionContainer.inl:38: undefined reference to curlpp::internal::SList::SList(std::__cxx11::list, std::allocator >, std::allocator, std::allocator > > > const&)' CMakeFiles/prtg_probe.dir/probe.cpp.o: In function curlpp::internal::OptionContainer, std::allocator >, std::allocator, std::allocator > > > ::setValue(std::__cxx11::list, std::allocator >, std::allocator, std::allocator > > > const&)': /usr/include/curlpp/internal/OptionContainer.inl:52: undefined reference to `curlpp::internal::SList::operator=(std::__cxx11::list, std::allocator >, std::allocator, std::allocator > > > const&)' .... ::OptionContainer(std::__cxx11::list, std::allocator >, std::allocator, std::allocator > > > const&)': /usr/include/curlpp/internal/OptionContainer.inl:38: 未定义参考 curlpp::internal::SList::SList(std::__cxx11::list, std::allocator >, std::allocator, std::allocator > > > const&)' CMakeFiles/prtg_probe.dir/probe。 cpp.o: 在函数 curlpp::internal::OptionContainer, std::allocator >, std::allocator, std::allocator > > > ::setValue(std::__cxx11::list, std::allocator >, std::allocator, std::allocator > > > const&)': /usr/include/curlpp/internal/OptionContainer.inl:52: 未定义对 `curlpp::internal::SList::operator=(std:: __cxx11::list, std::allocator >, std::allocator, std::allocator > > > const&)' ....

This is the code that produces the errors:这是产生错误的代码:

#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());
}

Every line with curlpp::options::... produces one error at link time.带有curlpp::options::...每一行curlpp::options::...在链接时产生一个错误。 I've looked around and searched Google for hours now, but all I found was to link libcurl together with libcurl.我环顾四周并在谷歌上搜索了几个小时,但我发现的只是将 libcurl 与 libcurl 链接在一起。 I do that, but still get this errors.我这样做了,但仍然收到此错误。

Here is the complete link line:这是完整的链接行:

/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

Does anybody know what I'm missing?有人知道我错过了什么吗? Do I have to add one more library and if yes which one?我是否必须再添加一个库,如果是,是哪一个?


I've tried to compile the example00.cpp with the following command:我尝试使用以下命令编译example00.cpp

g++ -o example00 example00.cpp -lm -lcurl -lcurlpp

and the result:结果:

/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

This looks like the same problem to me.这对我来说似乎是同样的问题。

The problem is that linkage of ccurlcpp::UnsetOption::UnsetOption is partially defective in the lipcurlcpp.so binary.问题是ccurlcpp::UnsetOption::UnsetOption链接在lipcurlcpp.so二进制文件中存在部分缺陷。

The linker's complaint with:链接者投诉:

g++ -o example00 example00.cpp -lm -lcurl -lcurlpp

is:是:

undefined reference to `curlpp::UnsetOption::UnsetOption(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

But if I demangle the constructor signatures in libcurlpp.so :但是,如果我对libcurlpp.so的构造函数签名进行libcurlpp.so

nm -D -C libcurlpp.so | grep UnsetOption::UnsetOption

I see:我懂了:

0000000000021776 T curlpp::UnsetOption::UnsetOption(char const*)
000000000002173e T curlpp::UnsetOption::UnsetOption(std::string const&)

The std::string hasn't been properly de-typedefed for some reason.由于某种原因, std::string没有被正确地定义类型。 If I get the source file in which this constructor is defined from the curlpp 0.7.3 source package, Exception.cpp , compile it:如果我从curlpp 0.7.3源包Exception.cpp获得定义此构造函数的源文件,请编译它:

curlpp-0.7.3/src/curlpp$ g++ -I../../include -I. -c Exception.cpp

and then demangle the constructor signatures from the object file:然后从对象文件中解压构造函数签名:

nm -C Exception.o | grep UnsetOption::UnsetOption

I get:我得到:

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&)

So:所以:

curlpp::UnsetOption::UnsetOption(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)

is the signature the compiler is telling the linker to look for, but that is not the signature in the library.是编译器告诉链接器要查找的签名,但这不是库中的签名。 The short explanation of the error is: the library is broken.错误的简短解释是:库坏了。

However, we see that no such inconsistency affects the other overload of the constructor:但是,我们看到没有这种不一致影响构造函数的其他重载:

curlpp::UnsetOption::UnsetOption(char const*)

nor could it, since the char const * is a builtin type.也不能,因为char const *是内置类型。

This enables a hack fix.这可以进行黑客修复。 The file in which the undefined-reference call is compiled is (as installed) /usr/include/curlpp/Option.inl , at the line:编译 undefined-reference 调用的文件是(安装时) /usr/include/curlpp/Option.inl ,位于以下行:

throw UnsetOption(std::string("You are trying to set an unset option to a handle"));

Edit this file, as root, and you see that it (inconsistently) contains two instances of:以 root 身份编辑此文件,您会看到它(不一致地)包含以下两个实例:

throw UnsetOption(std::string("blah blah"));

and one instance of:和一个实例:

throw UnsetOption("blah blah");

Change the occurrences of UnsetOption(std::string("blah blah")) to UnsetOption("blah blah") .将出现的UnsetOption(std::string("blah blah"))更改为UnsetOption("blah blah") Then only the good constructor is called in this file and example00 , at least, will compile and link.然后在这个文件中只调用好的构造函数,并且example00至少会编译和链接。

If you dislike the hack, or find that the problem re-surfaces elsewhere, then you may download the ubuntu source package curlpp_0.7.3.orig.tar.gz and build and install it yourself.如果您不喜欢 hack,或者发现问题在其他地方重新出现,那么您可以下载 ubuntu 源包curlpp_0.7.3.orig.tar.gzcurlpp_0.7.3.orig.tar.gz构建和安装。 That is the right remedy.这是正确的补救措施。

You can try to compile your project using old ABI:您可以尝试使用旧的 ABI 编译您的项目:
g++ -o example00 example00.cpp -D_GLIBCXX_USE_CXX11_ABI=0 -lm -lcurl -lcurlpp g++ -o example00 example00.cpp -D_GLIBCXX_USE_CXX11_ABI=0 -lm -lcurl -lcurlpp

For me the following worked on Ubuntu 18.04:对我来说,以下内容适用于 Ubuntu 18.04:

apt-get install libcurl4-openssl-dev
apt-get install libcurlpp-dev

Cmake:制作:

...
add_executable(HTTPPostTest HTTPPostTest.cpp)
TARGET_LINK_LIBRARIES(HTTPPostTest -lcurl -lcurlpp stdc++fs )
...

HTTPPostTest.cpp: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM