c++ - 用 gcc 编译 asio

标签 c++ gcc boost-asio

我试图从这里编译 HTTP 服务器 http://think-async.com/Asio/asio-1.11.0/doc/asio/examples/cpp11_examples.html#asio.examples.cpp11_examples.http_server与海合会。 我用过

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

我明白了

/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

有人知道为什么吗? 如果我尝试编译 http://think-async.com/Asio/asio-1.11.0/doc/asio/examples/cpp11_examples.html#asio.examples.cpp11_examples.allocation有效。

最佳答案

首先,我要说的是,您似乎提供了错误的编译命令来生成该错误消息,因为该错误消息表明您正在编译 server.cpp,例如:

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

当你实际上不是。事实上,您只是在编译 main.cpp,因为这是您给出的编译命令中唯一的 cpp 文件。

正如另一个答案所述,您需要编译 HTTP 服务器的 所有 .cpp 文件,而不仅仅是 main.cpp。为此,您只需将所有 .cpp 文件传递​​给您的 g++ 命令:

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

最后,与另一个答案所说的相反,您不需要 需要链接任何库,因为 Asio 是一个仅包含 header 的库。然而,Boost.Asio 并非仅包含 header ,因此如果您使用的是 Boost.Asio,则需要使用以下链接器标志链接到 Boost“系统”库:

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

(假设 Boost 库未安装在标准系统位置,例如 /lib;如果是,您可以省略 -L 标志. 正如评论中所述,-lboost_system 告诉链接器 (ld) 寻找名为 libboost_system.so 的库。参见 ld man page 了解有关 ld 如何查找库的更多信息。)

但是,正如您所说,因为您只使用 Asio 而不是 Boost.Asio,您只需包含适当的 .hpp 头文件并使用 -I 编译器标志指向到头文件位置。

关于c++ - 用 gcc 编译 asio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32869631/

相关文章:

c++ - 数组大小和常量

c++ - boost::asio::basic_waitable_timer `cancel` 行为

timer - ASIO 计时器 `cancel()` 可以调用虚假的 "success"吗?

C++ MinGW 共享库问题(仅限 Windows,适用于 Linux)?

C++ 嵌套类

c - 在 linux 中修改目标文件函数 xref 的优雅方式

c - GCC C 警告 "duplicate ‘const’ 声明“

c++ - 更好地提升 asio deadline_timer 示例

c++ - 工厂方法创建 shared_ptr 对象

c++ - 英特尔编译器无法识别来自 gcc 的 avxintrin.h 的标识符