boost::asio: Beispielcode funktioniert nicht



  • Hallöchen,

    für ein netzwerkfähiges Programm wollte ich mich mit boost::asio beschäftigen und habe daher den ersten Sockettutorialcode (http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/tutorial/tutdaytime1.html) in ein neues Codeblocks-Projekt kopiert.

    Als Compiler nutze ich MinGW32 unter Win7 Prof 32bit und von boost habe ich die Version 1.46.0.

    Hier noch einmal der Code:

    //
    // client.cpp
    // ~~~~~~~~~~
    //
    // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
    //
    // Distributed under the Boost Software License, Version 1.0. (See accompanying
    // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    //
    
    #include <iostream>
    #include <boost/array.hpp>
    #include <boost/asio.hpp>
    
    using boost::asio::ip::tcp;
    
    int main(int argc, char* argv[])
    {
      try
      {
        if (argc != 2)
        {
          std::cerr << "Usage: client <host>" << std::endl;
          return 1;
        }
    
        boost::asio::io_service io_service;
    
        tcp::resolver resolver(io_service);
        tcp::resolver::query query(argv[1], "daytime");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;
    
        tcp::socket socket(io_service);
        boost::system::error_code error = boost::asio::error::host_not_found;
        while (error && endpoint_iterator != end)
        {
          socket.close();
          socket.connect(*endpoint_iterator++, error);
        }
        if (error)
          throw boost::system::system_error(error);
    
        for (;;)
        {
          boost::array<char, 128> buf;
          boost::system::error_code error;
    
          size_t len = socket.read_some(boost::asio::buffer(buf), error);
    
          if (error == boost::asio::error::eof)
            break; // Connection closed cleanly by peer.
          else if (error)
            throw boost::system::system_error(error); // Some other error.
    
          std::cout.write(buf.data(), len);
        }
      }
      catch (std::exception& e)
      {
        std::cerr << e.what() << std::endl;
      }
    
      return 0;
    }
    

    Als Linklibraries habe ich bisher alles angegeben, was mir beim Lesen einiger Seiten zu boost::asio übern Weg gelaufen ist. In der Debug-Config sind das die Folgenden (Pfad zum Library-Ordner ist natürlich gesetzt):

    libboost_system-mgw45-mt-d-1_46.a
    libboost_thread-mgw45-mt-d-1_46.a
    libboost_date_time-mgw45-mt-d-1_46.a
    libboost_regex-mgw45-mt-d-1_46.a
    libboost_wserialization-mgw45-mt-d-1_46.a
    

    Meine Ausgabe sieht dennoch folgendermaßen aus:

    ..\boost\include\boost-1_46\boost\asio\detail\config.hpp|59|warning: #warning Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately.|
    ..\boost\include\boost-1_46\boost\asio\detail\config.hpp|60|warning: #warning For example, add -D_WIN32_WINNT=0x0501 to the compiler command line.|
    ..\boost\include\boost-1_46\boost\asio\detail\config.hpp|61|warning: #warning Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).|
    ..\boost\include\boost-1_46\boost\asio\ip\impl\address_v6.ipp||In constructor 'boost::asio::ip::address_v6::address_v6()':|
    ..\boost\include\boost-1_46\boost\asio\ip\impl\address_v6.ipp|37|warning: missing braces around initializer for 'in6_addr::<anonymous union>'|
    ..\boost\include\boost-1_46\boost\asio\ip\impl\address_v6.ipp|37|warning: missing braces around initializer for 'u_char [16]'|
    ..\boost\include\boost-1_46\boost\asio\ip\impl\address_v6.ipp|259|warning: missing braces around initializer for 'in6_addr::<anonymous union>'|
    ..\boost\include\boost-1_46\boost\asio\ip\impl\address_v6.ipp|259|warning: missing braces around initializer for 'u_char [16]'|
    ..\boost\include\boost-1_46\boost\asio\ip\detail\impl\endpoint.ipp||In constructor 'boost::asio::ip::detail::endpoint::endpoint(int, short unsigned int)':|
    ..\boost\include\boost-1_46\boost\asio\ip\detail\impl\endpoint.ipp|60|warning: missing braces around initializer for 'in6_addr::<anonymous union>'|
    ..\boost\include\boost-1_46\boost\asio\ip\detail\impl\endpoint.ipp|60|warning: missing braces around initializer for 'u_char [16]'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\winsock_init.ipp|39|undefined reference to `WSAStartup@8'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\winsock_init.ipp|48|undefined reference to `WSACleanup@0'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|50|undefined reference to `WSASetLastError@4'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|285|undefined reference to `ioctlsocket@12'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|315|undefined reference to `closesocket@4'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|607|undefined reference to `WSARecv@28'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|876|undefined reference to `WSASend@28'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|1102|undefined reference to `WSASocketA@24'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|1113|undefined reference to `setsockopt@20'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|1520|undefined reference to `select@20'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|1595|undefined reference to `select@20'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|2663|undefined reference to `WSAGetLastError@0'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|2681|undefined reference to `getaddrinfo@16'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|2724|undefined reference to `freeaddrinfo@4'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|61|undefined reference to `WSAGetLastError@0'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|61|undefined reference to `WSAGetLastError@0'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|380|undefined reference to `connect@12'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|1148|undefined reference to `setsockopt@20'|
    obj\Debug\main.o:G:\C++\Zeit\..\boost\include\boost-1_46\boost\asio\detail\impl\socket_ops.ipp|1234|undefined reference to `getsockopt@20'|
    ||=== Build finished: 19 errors, 9 warnings (0 minutes, 6 seconds) ===|
    

    Da scheint wohl noch eine Lib zu fehlen, oder wo könnte sonst das Problem liegen?

    Muss ehrlich sagen, dass ich mit der Doku von boost sehr unzufrieden bin. Entweder werden wichtige Dinge verschwiegen (so war's bei boost::thread, dass es wichtig ist einen define-Wert zu setzen, von dem in der Doku nirgends die Rede war und ich nur durch ein Forenmitglied drauf kam) oder sie sind so versteckt, dass man sich erstmal durch tausend Seiten klicken muss, bis man drauf stößt 😞



  • Du musst noch gegen ws2_32.lib linken.



  • fdfdg schrieb:

    Du musst noch gegen ws2_32.lib linken.

    Die Lib gehört aber nicht zu boost, seh ich das richtig?

    **edit:
    Gehört zu Windows. Und genau sowas mein ich. Wo steht das in der boost-Doku? Da steht ja nichtmal wirklich welche boost-eigenen Libs ich nun für das genannte Beispiel brauche 😕

    Und natürlich: muchas gracias 😉



  • Subsurf schrieb:

    Gehört zu Windows. Und genau sowas mein ich. Wo steht das in der boost-Doku? Da steht ja nichtmal wirklich welche boost-eigenen Libs ich nun für das genannte Beispiel brauche 😕

    Für Anfänger ist das bestimmt nicht so einfach - andererseits hast Du ja jetzt gelernt, dass Du mit den nicht aufgelösten Symbolen leicht die zugehörige lib findest. Ist also nur das erstemal dass dich das aufhält. 🕶

    BTW: Hättest Du VS genommen hätte es die entsprechenden Libs automatisch dazugelinkt.

    Simon


Anmelden zum Antworten