Pedro Vieira
Pedro Vieira

Reputation: 2316

Running HTTP server example from Boost Asio

I'm getting errors when trying to run the HTTP server example that comes with the source of the boost library, under the path: boost_1_59_0/libs/asio/example/cpp11/http/server/.

I already ran this following commands in the boost_1_59_0 directory:

After installing all targets, i tried to compile the main.cpp and the server.cpp with this command: g++ -std=c++0x -o main main.cpp -I "/home/user/Desktop/boost_1_59_0" -L "/home/user/Desktop/boost_1_59_0/libs/" -lboost_system. Any suggestion on how to compile this server example?

I linked all files from the boost_1_59_0/libs/asio/example/cpp11/http/server/ folder after the main.cpp, as @Richard Hodges suggested. It still didn't work, i got errors concerning lpthread, so i added it to the compiling options. The program compiled but it failed the execution, returning an error saying that it didn't find the library libboost_system.so.1.59.0. I tried linking the folders with -L /path/to/library but it didn't work.

Solution:

My compilation command:

g++ -std=gnu++0x -o main main.cpp server.cpp connection.cpp connection_manager.cpp reply.cpp mime_types.cpp request_handler.cpp request_parser.cpp -I "/home/user/Desktop/boost_1_59_0" -lboost_system -lpthread

I solved it with this commands:

And then I just ran the executable and it worked!

Upvotes: 3

Views: 996

Answers (1)

sehe
sehe

Reputation: 393134

Here's a simple makefile I just concocted that works:

all:server

CPPFLAGS+=-std=c++11 -Wall -pedantic
CPPFLAGS+=-g -O2

CPPFLAGS+=-pthread
LDFLAGS+=-lboost_system

%.o:%.cpp
    $(CXX) $(CPPFLAGS) $^ -c -o $@

server:$(patsubst %.cpp,%.o,$(wildcard *.cpp))
    $(CXX) $(CPPFLAGS) $^ -o $@ $(LDFLAGS)

It runs make:

g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection.cpp -c -o connection.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection_manager.cpp -c -o connection_manager.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread main.cpp -c -o main.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread mime_types.cpp -c -o mime_types.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread reply.cpp -c -o reply.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread request_handler.cpp -c -o request_handler.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread request_parser.cpp -c -o request_parser.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread server.cpp -c -o server.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection.o connection_manager.o main.o mime_types.o reply.o request_handler.o request_parser.o server.o -o server -lboost_system

And the test program runs:

$ ./server 0.0.0.0 9889 . & 
$ GET http://localhost:9889/main.cpp > main.cpp.0

Check the files

$ md5sum main.cpp*
be5dc1c26b5942101a7895de6baedcee  main.cpp
be5dc1c26b5942101a7895de6baedcee  main.cpp.0

Don't forget to kill the server when you're done

Upvotes: 2

Related Questions