Oana Andone Oana Andone - 4 months ago 25
C++ Question

Compile asio with gcc

I tried to compile HTTP Server from here http://think-async.com/Asio/asio-1.11.0/doc/asio/examples/cpp11_examples.html#asio.examples.cpp11_examples.http_server with gcc.
I used

g++ main.cpp -std=c++11 -I/home/gabi/Downloads/asio-1.11.0/include -pthread


I get this

/tmp/ccE1vIzF.o: In function `http::server::server::server(std::string const&, std::string const&, std::string const&)':
server.cpp:(.text+0x1a3): undefined reference to `http::server::connection_manager::connection_manager()'
server.cpp:(.text+0x1e0): undefined reference to `http::server::request_handler::request_handler(std::string const&)'
/tmp/ccE1vIzF.o: In function `http::server::server::do_accept()::{lambda(std::error_code)#1}::operator()(std::error_code) const':
server.cpp:(.text+0x52a): undefined reference to `http::server::connection_manager::start(std::shared_ptr<http::server::connection>)'
/tmp/ccE1vIzF.o: In function `http::server::server::do_await_stop()::{lambda(std::error_code, int)#1}::operator()(std::error_code, int) const':
server.cpp:(.text+0x5e9): undefined reference to `http::server::connection_manager::stop_all()'
/tmp/ccE1vIzF.o: In function `_ZN9__gnu_cxx13new_allocatorIN4http6server10connectionEE9constructIS3_IN4asio19basic_stream_socketINS6_2ip3tcpENS6_21stream_socket_serviceIS9_EEEERNS2_18connection_managerERNS2_15request_handlerEEEEvPT_DpOT0_':
server.cpp:(.text._ZN9__gnu_cxx13new_allocatorIN4http6server10connectionEE9constructIS3_IN4asio19basic_stream_socketINS6_2ip3tcpENS6_21stream_socket_serviceIS9_EEEERNS2_18connection_managerERNS2_15request_handlerEEEEvPT_DpOT0_[_ZN9__gnu_cxx13new_allocatorIN4http6server10connectionEE9constructIS3_IN4asio19basic_stream_socketINS6_2ip3tcpENS6_21stream_socket_serviceIS9_EEEERNS2_18connection_managerERNS2_15request_handlerEEEEvPT_DpOT0_]+0x8b): undefined reference to `http::server::connection::connection(asio::basic_stream_socket<asio::ip::tcp, asio::stream_socket_service<asio::ip::tcp> >, http::server::connection_manager&, http::server::request_handler&)'
collect2: error: ld returned 1 exit status


Does anyone know why?
if I try to compile http://think-async.com/Asio/asio-1.11.0/doc/asio/examples/cpp11_examples.html#asio.examples.cpp11_examples.allocation works.

Answer

First off, let me say that it seems you have provided the wrong compilation command to produce that error message, since the error message indicates that you're compiling server.cpp, e.g.:

server.cpp:(.text+0x1a3): undefined reference to 'http::server::connection_manager::connection_manager()'

when you're actually not. In fact, you're only compiling main.cpp, since that's the only cpp file that's in the compilation command you gave.

As another answer states, you need to compile all of the .cpp files for the HTTP server, not just main.cpp. To do that, you simply pass all of the .cpp files to your g++ command:

g++ main.cpp connection.cpp connection_manager.cpp mime_types.cpp reply.cpp request_handler.cpp request_parser.cpp server.cpp -std=c++11 -I/home/gabi/Downloads/asio-1.11.0/include -pthread

And finally, contrary to what another answer says, you do not need to link against any libraries, because Asio is a header-only library. Boost.Asio, however, is not header-only, and so if you were using Boost.Asio, you'd need to link to the Boost 'System' library with the following linker flags:

-L/path/to/folder/containing/Boost/libs/ -lboost_system

(This is assuming that the Boost libraries are not installed in a standard system location, such as /lib; if they were, you could omit the -L flag. As was stated in a comment, -lboost_system tells the linker (ld) to look for a library called libboost_system.so. See the ld man page for more information on how ld finds libraries.)

However, since, as you said, you are only using Asio and not Boost.Asio, you are fine with just including the appropriate .hpp header files and using the -I compiler flag to point to the header file locations.

Comments