0

代码是这样的

#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-17ubuntu1~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=gnu++2a' 或 '-std=c++2a',两者都不起作用。

4

0 回答 0