gcc 对std::experimental::net的未定义引用

dy1byipe  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(184)

代码是这样的

#include <experimental/net>
#include <iostream>
#include <string>
#include <chrono>

namespace net = std::experimental::net;
using namespace std::chrono_literals;

int main()
{
    std::cout << "networking ts stream example" << std::endl;

    net::ip::tcp::iostream s;

    s.expires_after(5s);
    s.connect("ipecho.net", "http");

    if(!s) {
        std::cout << "error: " << s.error().message() << std::endl;
        return -1;
    }

    s << "GET /plain HTTP/1.0\r\n";
    s << "Host: ipecho.net\r\n";
    s << "Accept: */*\r\n";
    s << "Connection: close\r\n\r\n";

    std::string header;
    while(s && std::getline(s, header) && header != "\r")
        std::cout << header << "\n";

    std::cout << s.rdbuf();
    std::cout << std::endl;
}

我试过用tdm-gcc 10,fedora g++(GCC)11.2.1和g++(Ubuntu 9.3.0- 17 ubuntu 1 ~20.04)9.3.0编译,都失败了,输出如下:

/usr/bin/ld: /tmp/ccUtsmxu.o: in function `std::experimental::net::v1::basic_socket_iostream<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::basic_socket_iostream()':
a.cpp:(.text._ZNSt12experimental3net2v121basic_socket_iostreamINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEC1Ev[_ZNSt12experimental3net2v121basic_socket_iostreamINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEC1Ev]+0xd1): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::~basic_socket_streambuf()'
/usr/bin/ld: /tmp/ccUtsmxu.o: in function `void std::experimental::net::v1::basic_socket_iostream<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::connect<char const (&) [10], char const (&) [5]>(char const (&) [10], char const (&) [5])':
a.cpp:(.text._ZNSt12experimental3net2v121basic_socket_iostreamINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEE7connectIJRA10_KcRA5_SC_EEEvDpOT_[_ZNSt12experimental3net2v121basic_socket_iostreamINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEE7connectIJRA10_KcRA5_SC_EEEvDpOT_]+0x4f): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >* std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::connect<char const (&) [10], char const (&) [5]>(char const (&) [10], char const (&) [5])'
/usr/bin/ld: /tmp/ccUtsmxu.o: in function `std::experimental::net::v1::basic_socket_iostream<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::~basic_socket_iostream()':
a.cpp:(.text._ZNSt12experimental3net2v121basic_socket_iostreamINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEED1Ev[_ZNSt12experimental3net2v121basic_socket_iostreamINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEED1Ev]+0x4d): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::~basic_socket_streambuf()'
/usr/bin/ld: /tmp/ccUtsmxu.o:(.data.rel.ro._ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE[_ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE]+0x10): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::~basic_socket_streambuf()'
/usr/bin/ld: /tmp/ccUtsmxu.o:(.data.rel.ro._ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE[_ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE]+0x18): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::~basic_socket_streambuf()'
/usr/bin/ld: /tmp/ccUtsmxu.o:(.data.rel.ro._ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE[_ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE]+0x28): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::setbuf(char*, long)'
/usr/bin/ld: /tmp/ccUtsmxu.o:(.data.rel.ro._ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE[_ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE]+0x40): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::sync()'
/usr/bin/ld: /tmp/ccUtsmxu.o:(.data.rel.ro._ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE[_ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE]+0x58): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::underflow()'
/usr/bin/ld: /tmp/ccUtsmxu.o:(.data.rel.ro._ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE[_ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE]+0x68): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::pbackfail(int)'
/usr/bin/ld: /tmp/ccUtsmxu.o:(.data.rel.ro._ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE[_ZTVNSt12experimental3net2v122basic_socket_streambufINS1_2ip3tcpENSt6chrono3_V212steady_clockENS1_11wait_traitsIS7_EEEE]+0x78): undefined reference to `std::experimental::net::v1::basic_socket_streambuf<std::experimental::net::v1::ip::tcp, std::chrono::_V2::steady_clock, std::experimental::net::v1::wait_traits<std::chrono::_V2::steady_clock> >::overflow(int)'
collect2: error: ld returned 1 exit status

他们怎么了?我的编译器选项是'-std=gnu2a'或'-std=c2a',两者都不起作用。

b5lpy0ml

b5lpy0ml1#

对于GCC 13,似乎必须使用标志-lstdc++exp进行链接
来自GCC文档:https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dynamic_or_shared.html#manual.intro.using.linkage.experimental
GCC 13包含由P1429 R3定义的C合同库的实现。因为这是一个实验性的扩展,不是C标准的一部分,所以它是在一个单独的库libstdc ++ exp. a中实现的,并且没有共享库。要使用这个库,你应该包含<experimental/contract>并使用-lstdc++exp链接。

相关问题