Yritin Hello_worldin kääntämistä, mutta
$ g++ lbhttpser01.cpp -o lbhttpser01 -Wall -pedantic -L/usr/local/lib -pthread -std=c++17
/usr/bin/ld: /tmp/ccTd04do.o: in function `hello_world_resource::render(httpserver::http_request const&)':
lbhttpser01.cpp:(.text+0xb6): undefined reference to `httpserver::http_request::get_arg(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x1e9): undefined reference to `httpserver::http::http_utils::text_plain'
/usr/bin/ld: /tmp/ccTd04do.o: in function `main':
lbhttpser01.cpp:(.text+0x41b): undefined reference to `httpserver::webserver::webserver(httpserver::create_webserver const&)'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x487): undefined reference to `httpserver::webserver::register_resource(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, httpserver::http_resource*, bool)'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x4b9): undefined reference to `httpserver::webserver::start(bool)'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x4dc): undefined reference to `httpserver::webserver::~webserver()'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x575): undefined reference to `httpserver::webserver::~webserver()' ..... jne
Olisiko tehnyt virheen (taas
) siinä, että laitoin github kloonauksen alihakemistorakenteeseen näin
/home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/
ja
käännettävän ohjelman paikkaan
~/Documents/my_libhttpserver/kokeilut/
Virheimitushan voisi tarkoittaa, että oikeaa (viittaus)tiedostoa ei löydy mistään. Ehkä libhttpserverin asennuksessa käytetyt make ja make install olettavat jotain hakemistorakenteesta?
Hello_world on tässä alla libhttpserver01.cpp -nimisenä.
#include <iostream>
#include "/home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/src/httpserver.hpp"
class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
void set_some_data(const std::string &s) {data = s;}
std::string data;
};
// Using the render method you are able to catch each type of request you receive
const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req) {
// It is possible to store data inside the resource object that can be altered through the requests
std::cout << "Data was: " << data << std::endl;
std::string datapar = req.get_arg("data");
set_some_data(datapar == "" ? "no data passed!!!" : datapar);
std::cout << "Now data is:" << data << std::endl;
// It is possible to send a response initializing an http_string_response that reads the content to send in response from a string.
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello World!!!", 200));
}
int main() {
// It is possible to create a webserver passing a great number of parameters. In this case we are just passing the port and the number of thread running.
httpserver::webserver ws = httpserver::create_webserver(8080).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5);
hello_world_resource hwr;
// This way we are registering the hello_world_resource to answer for the endpoint
// "/hello". The requested method is called (if the request is a GET we call the render_GET
// method. In case that the specific render method is not implemented, the generic "render"
// method is called.
ws.register_resource("/hello", &hwr, true);
// This way we are putting the created webserver in listen. We pass true in order to have
// a blocking call; if we want the call to be non-blocking we can just pass false to the method.
ws.start(true);
return 0;
}