// 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文件夹之外运行此示例,但是当我尝试复制该代码并将其放在自己的文件夹中时,它不会编译。
/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
最佳答案
注意,在根目录下的CMakeLists.txt中,Asio有1行
include_directories(deps/asio/asio/include)
将其添加到您的项目中,然后它将起作用
关于c++ - 我从cpp-netlib获得的示例代码无法编译,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33050929/