1

我是 boost 的初学者,并试图从这个链接编译 c++ 程序:http: //www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/examples.html#boost_asio.examples.http_server_3 (httpserver3例如),所以我尝试使用这个命令(在 linux [debian] 中)编译这个项目:

g++ -o htserv.exe connection.cpp main.cpp mime_types.cpp request_handler.cpp request_parser.cpp server.cpp

或者

g++ -Wall -g -I /usr/local/include/boost connection.cpp main.cpp mime_types.cpp request_handler.cpp request_parser.cpp server.cpp -o htserv

然后我看到这个错误:

In file included from connection.cpp:11:0:
connection.hpp:20:23: fatal error: request.hpp: No such file or directory
compilation terminated.
In file included from server.hpp:19:0,
                 from main.cpp:16:
connection.hpp:20:23: fatal error: request.hpp: No such file or directory
compilation terminated.
request_handler.cpp:18:23: fatal error: request.hpp: No such file or directory
compilation terminated.
request_parser.cpp:12:23: fatal error: request.hpp: No such file or directory
compilation terminated.
In file included from server.hpp:19:0,
                 from server.cpp:11:
connection.hpp:20:23: fatal error: request.hpp: No such file or directory

是的,你说得对,我检查了权限,当我编译时遇到了一个新问题,我很困惑

/tmp/ccj4xp5E.o: In function `http::server3::connection::handle_read(boost::system::error_code const&, unsigned long)':
/home/avakh/Desktop/boost/httpserver3/connection.cpp:56: undefined reference to `http::server3::reply::to_buffers()'
/home/avakh/Desktop/boost/httpserver3/connection.cpp:60: undefined reference to `http::server3::reply::stock_reply(http::server3::reply::status_type)'
/home/avakh/Desktop/boost/httpserver3/connection.cpp:64: undefined reference to `http::server3::reply::to_buffers()'
/tmp/ccj4xp5E.o: In function `__static_initialization_and_destruction_0':
/usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
/tmp/ccj4xp5E.o: In function `boost::system::error_code::error_code()':
/usr/local/include/boost/system/error_code.hpp:323: undefined reference to `boost::system::system_category()'
/tmp/ccj4xp5E.o: In function `boost::asio::error::get_system_category()':
/usr/local/include/boost/asio/error.hpp:216: undefined reference to `boost::system::system_category()'
/tmp/ccH8QGhE.o: In function `__static_initialization_and_destruction_0':
/usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
/tmp/cclaofcI.o: In function `http::server3::request_handler::handle_request(http::server3::request const&, http::server3::reply&)':
/home/avakh/Desktop/boost/httpserver3/request_handler.cpp:34: undefined reference to `http::server3::reply::stock_reply(http::server3::reply::status_type)'
/home/avakh/Desktop/boost/httpserver3/request_handler.cpp:42: undefined reference to `http::server3::reply::stock_reply(http::server3::reply::status_type)'
/home/avakh/Desktop/boost/httpserver3/request_handler.cpp:66: undefined reference to `http::server3::reply::stock_reply(http::server3::reply::status_type)'
/tmp/cclaofcI.o: In function `__static_initialization_and_destruction_0':
/usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
/tmp/ccJ7jCu1.o: In function `__static_initialization_and_destruction_0':
/usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
/tmp/ccJ7jCu1.o: In function `boost::asio::detail::posix_signal_blocker::posix_signal_blocker()':
/usr/local/include/boost/asio/detail/posix_signal_blocker.hpp:43: undefined reference to `pthread_sigmask'
/tmp/ccJ7jCu1.o: In function `boost::asio::detail::posix_signal_blocker::~posix_signal_blocker()':
/usr/local/include/boost/asio/detail/posix_signal_blocker.hpp:50: undefined reference to `pthread_sigmask'
/tmp/ccJ7jCu1.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
/usr/local/include/boost/asio/detail/impl/posix_thread.ipp:35: undefined reference to `pthread_detach'
/tmp/ccJ7jCu1.o: In function `boost::asio::detail::posix_thread::join()':
/usr/local/include/boost/asio/detail/impl/posix_thread.ipp:42: undefined reference to `pthread_join'
/tmp/ccJ7jCu1.o: In function `boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)':
/usr/local/include/boost/asio/detail/impl/posix_thread.ipp:50: undefined reference to `pthread_create'
/tmp/ccJ7jCu1.o: In function `boost::thread_exception::thread_exception(int, char const*)':
/usr/local/include/boost/thread/exceptions.hpp:51: undefined reference to `boost::system::system_category()'
/tmp/ccJ7jCu1.o: In function `boost::detail::thread_data_base::thread_data_base()':
/usr/local/include/boost/thread/pthread/thread_data.hpp:144: undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccJ7jCu1.o: In function `boost::thread::start_thread()':
/usr/local/include/boost/thread/detail/thread.hpp:180: undefined reference to `boost::thread::start_thread_noexcept()'
/tmp/ccJ7jCu1.o: In function `boost::thread::~thread()':
/usr/local/include/boost/thread/detail/thread.hpp:255: undefined reference to `boost::thread::detach()'
/tmp/ccJ7jCu1.o: In function `boost::thread::get_id() const':
/usr/local/include/boost/thread/detail/thread.hpp:730: undefined reference to `boost::thread::native_handle()'
/tmp/ccJ7jCu1.o: In function `boost::thread::join()':
/usr/local/include/boost/thread/detail/thread.hpp:756: undefined reference to `boost::thread::join_noexcept()'
/tmp/ccJ7jCu1.o: In function `boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::~thread_data()':
/usr/local/include/boost/thread/detail/thread.hpp:91: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccJ7jCu1.o:(.rodata._ZTIN5boost6detail11thread_dataINS_3_bi6bind_tImNS_4_mfi3mf0ImNS_4asio10io_serviceEEENS2_5list1INS2_5valueIPS7_EEEEEEEE[_ZTIN5boost6detail11thread_dataINS_3_bi6bind_tImNS_4_mfi3mf0ImNS_4asio10io_serviceEEENS2_5list1INS2_5valueIPS7_EEEEEEEE]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: error: ld returned 1 exit status

然后我尝试使用此命令进行编译: gcc -I /usr/local/include/boost/ -lboost_thread -lboost-asio -lboost_noncopyable -lboost_bind -lboost_logic -lboost_tuple -lboost_array connection.cpp mime_types.cpp main.cpp reply.cpp request_handler.cpp server.cpp -o hserv 我有这个错误:

/usr/bin/ld: cannot find -lboost-asio
/usr/bin/ld: cannot find -lboost_noncopyable
/usr/bin/ld: cannot find -lboost_bind
/usr/bin/ld: cannot find -lboost_logic
/usr/bin/ld: cannot find -lboost_tuple
/usr/bin/ld: cannot find -lboost_array
collect2: error: ld returned 1 exit status

然后我尝试使用这个命令 g++ -I /usr/local/include/boost/connection.cpp mime_types.cpp main.cpp reply.cpp request_handler.cpp server.cpp -o hserv -lboost-asio -lboost_noncopyable -lboost_bind -lboost_logic -lboost_tuple -lboost_array

这有错误

/usr/bin/ld: cannot find -lboost-asio
/usr/bin/ld: cannot find -lboost_noncopyable
/usr/bin/ld: cannot find -lboost_bind
/usr/bin/ld: cannot find -lboost_logic
/usr/bin/ld: cannot find -lboost_tuple
/usr/bin/ld: cannot find -lboost_array
collect2: error: ld returned 1 exit status
4

2 回答 2

2

您的编译命令可能应该以g++ -Wall -g -I/usr/include/boost

where-Wall询问所有警告,-g询问调试信息,-I/usr/include/boost并将包含目录添加到包含路径。

你真的应该使用像GNU Make这样的构建器。是时候学习如何编写Makefile. 然后你应该添加你Makefile的一行

  CXXFLAGS= -Wall -g -I/usr/include/boost

如果request.hpp当前目录中是您自己的文件(不是某个系统库中的文件),则应将其包含在双引号中,例如

  #include "request.hpp"

并且不要使用<request.hpp>或添加-I.到您的编译命令中。

确保使用pwdls命令在正确的目录中运行编译。该ls命令应该同时显示request.hppconnection.cpp。如果需要,使用cd命令(调用chdir(2)系统调用)。花时间阅读有关bash 脚本的更多信息

您可能希望将-v(显示为编译启动的真实程序)和-H(显示包含的文件)传递给您的编译器。

请记住,g++编译器的参数顺序很重要;源文件应该放在目标文件和库之前,并且都应该从高级到低级(例如系统库)。阅读有关调用 GCC的章节。您可能需要一些-L选项。

顺便说一句,有几个可用的 HTTP 服务器库,例如libonion(它同时支持 HTTP 和 HTTPS)。

于 2013-09-09T05:42:32.090 回答
0

我在用 Ogre 编译时遇到了同样的问题。我将 /usr/local/lib 下的所有 boost 文件(.a 和 .so)复制到 /usr/lib,它可以工作。

于 2015-02-26T13:51:29.673 回答