2015-10-10 2 views
0
// Copyright 2009 (c) Tarro, Inc. 
// Copyright 2009 (c) Dean Michael Berris <[email protected]> 
// 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) 
// 

//[ hello_world_server_main 
/*` 
    This is a part of the 'Hello World' example. It's used to 
    demonstrate how easy it is to set up an HTTP server. All we do in 
    this example is create a request handler and run the server. 
*/ 
#include <boost/network/protocol/http/server.hpp> 
#include <iostream> 


namespace http = boost::network::http; 


/*<< Defines the server. >>*/ 
struct hello_world; 
typedef http::server<hello_world> server; 

/*<< Defines the request handler. It's a class that defines two 
    functions, `operator()` and `log()` >>*/ 
struct hello_world { 
    /*<< This is the function that handles the incoming request. >>*/ 
    void operator() (server::request const &request, 
        server::response &response) { 
     server::string_type ip = source(request); 
     std::ostringstream data; 
     data << "Hello, " << ip << "!"; 
     response = server::response::stock_reply(
      server::response::ok, data.str()); 
    } 
    /*<< It's necessary to define a log function, but it's ignored in 
     this example. >>*/ 
    void log(...) { 
     // do nothing 
    } 
}; 


int main(int argc, char * argv[]) { 

    if (argc != 3) { 
     std::cerr << "Usage: " << argv[0] << " address port" << std::endl; 
     return 1; 
    } 

    try { 
     /*<< Creates the request handler. >>*/ 
     hello_world handler; 
     /*<< Creates the server. >>*/ 
     server server_(argv[1], argv[2], handler); 
     /*<< Runs the server. >>*/ 
     server_.run(); 
    } 
    catch (std::exception &e) { 
     std::cerr << e.what() << std::endl; 
     return 1; 
    } 

    return 0; 
} 
//] 

Я подозреваю, что это связано с неправильной ссылкой на мои библиотеки. Это ошибка, которую я получаю, когда запускаю make. Я могу запустить этот пример, удалив папку cpp-netlib, но когда я попытаюсь скопировать этот код и поместить его в свою папку, он не компилируется.Образец кода, который я получил от cpp-netlib, не будет компилироваться

/home/stanley/cmpt373/textadventure/tools/simple_server_test/simpleServerTest.cpp: In function ‘int main(int, char**)’: 
    /home/stanley/cmpt373/textadventure/tools/simple_server_test/simpleServerTest.cpp:56:49: error: no matching function for call to ‘boost::network::http::server<hello_world>::server(char*&, char*&, hello_world&)’ 
      server server_(argv[1], argv[2], handler); 
                ^
    /home/stanley/cmpt373/textadventure/tools/simple_server_test/simpleServerTest.cpp:56:49: note: candidate is: 
    In file included from /home/stanley/cmpt373/textadventure/tools/simple_server_test/simpleServerTest.cpp:14:0: 
    /home/stanley/cmpt373/textadventure/include/boost/network/protocol/http/server.hpp:44:12: note: boost::network::http::server<Handler>::server(const options&) [with Handler = hello_world; boost::network::http::server<Handler>::options = boost::network::http::server_options<boost::network::http::tags::http_server, hello_world>] 
     explicit server(options const &options) : server_base(options) {} 
       ^
    /home/stanley/cmpt373/textadventure/include/boost/network/protocol/http/server.hpp:44:12: note: candidate expects 1 argument, 3 provided 
    make[2]: *** [tools/simple_server_test/CMakeFiles/simpleServerTest.dir/simpleServerTest.cpp.o] Error 1 
    make[1]: *** [tools/simple_server_test/CMakeFiles/simpleServerTest.dir/all] Error 2 
    make: *** [all] Error 2 
+0

Так как этот пример кода с 2009 года вам может понадобиться более старая версия boost для ее компиляции как есть, в противном случае вам нужно исправить проблему с примером кода, который потребует некоторого копания. –

+0

Просто догадка: код примера был написан для другой версии Boost и не обновлялся с тех пор. Проверьте систему отслеживания ошибок Boost и, возможно, зарегистрируйте соответствующую ошибку. –

+0

Я могу создать и запустить пример из папки cpp-netlib – user3229707

ответ

1

Примечание в CMakeLists.txt в корневой директории, есть одна линия для Asio

include_directories(deps/asio/asio/include) 

добавить его в свой проект, то он будет работать

Смежные вопросы