我有以下代码使用Boost ASIO来设置TCP客户端。以下是我的代码改编自Boost doc chat example。
class AsioCommunicationService {
AsioCommunicationService::AsioCommunicationService(
boost::asio::io_service& io_service,
tcp::resolver::iterator endpoint_iterator)
: io_service_(io_service),
socket_(io_service)
{
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
boost::bind(&AsioCommunicationService::handle_connect, this,
boost::asio::placeholders::error, ++endpoint_iterator));
}
void AsioCommunicationService::handle_connect(const boost::system::error_code& error,
tcp::resolver::iterator endpoint_iterator)
{
if (!error)
{
boost::asio::async_read(socket_,
boost::asio::buffer(read_msg_.data(), LampMessage::header_length),
boost::bind(&AsioCommunicationService::handle_read_header, this,
boost::asio::placeholders::error));
}
}
}
class Connection
{
//init io_service, query, resolve, iterator here
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(host, service);
boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
resolver.resolve(query);
m_session = std::shared_ptr<AsioCommunicationService>(
new AsioCommunicationService(io_service, iterator));
//start new thread for io_service.run --> GOT AN ERROR when include boost/thread.hpp
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
//this synchronous command would work, but it's blocking the program. I don't want that.
//io_service.run();
}
当然,我需要包含boost / thread来在类Connection中声明变量t。但是当我这样做时,我收到了这个错误
#include <boost/thread.hpp>
//ERROR: In function ‘boost::thread&& boost::move(boost::thread&&)’:
///usr/include/boost/thread/detail/thread.hpp:349:16: error: invalid initialization of reference of type ‘boost::thread&&’ from expression of type ‘boost::thread’
//In file included from /usr/include/boost/thread/detail/thread_heap_alloc.hpp:17:0,
// from /usr/include/boost/thread/detail/thread.hpp:13,
// from /usr/include/boost/thread/thread.hpp:22,
// from /usr/include/boost/thread.hpp:13,
// from /home/son/dev/logistics/src/frameworks/networkService/NetworkConnection.cpp:13:
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp: In function ‘T* boost::detail::heap_new(A1&&) [with T = boost::detail::thread_data<void (*)()>, A1 = void (*&)()]’:
///usr/include/boost/thread/detail/thread.hpp:130:95: instantiated from here
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp:24:47: error: cannot bind ‘void (*)()’ lvalue to ‘void (*&&)()’
///usr/include/boost/thread/detail/thread.hpp:43:13: error: initializing argument 1 of ‘boost::detail::thread_data<F>::thread_data(F&&) [with F = void (*)()]’
如果我将include移除到boost / thread.hpp,并通过简单调用io_service.run()将声明替换为t,它将编译并工作;我想知道这个编译错误是否与boost版本有关。我正在使用Boost ASIO 1.42,Ubuntu 11.04和Eclipse,如果这些有任何帮助的话。提前谢谢。
答案 0 :(得分:3)
我写了一个包含单个include指令的文件:
#include <boost/thread.hpp>
g++-4.5.4 -std=c++0x -I /usr/include/boost-1_42 -c
提出了您提到的错误。g++-4.6.3 -std=c++0x -I /usr/include/boost-1_42 -c
相同的g++-4.7.1 -std=c++0x -I /usr/include/boost-1_42 -c
提供了更多错误g++-4.7.1 -std=c++0x -I /usr/include/boost-1_49 -c
没有一个错误g++-4.6.3
和g++-4.5.4
使用boost 1.49 所以我真的建议你使用更新版本的boost。您不必在系统范围内安装它,而是可以为单个用户安装它。所以你不依赖于ubuntu软件包。
要手动安装提升,建议您按照Getting Started on Unix Variants指南进行操作:
boost_1_50_0.tar.bz2
到某个临时源目录,cd
进入./bootstrap.sh --prefix=${HOME}/boost_1_50 && ./b2 install
-I ${HOME}/boost_1_50
获取正确的标题${HOME}/boost_1_50/lib/libboost_thread.a
作为参数
醇>
使用静态libboost_thread.a
而不是动态libbtoost_thread.so
将确保您不必担心定位库以启动应用程序。您需要的所有内容都将包含在您的主二进制文件中。
答案 1 :(得分:1)
按照C ++ 11标准的@MvG推理,我做了一些研究。似乎在Uccntu Natty提供的gcc和boost之间是a problem of coordination,它分别使用-std = c ++ 0x和libboost1.42。
没有解决方案,但我使用了这种解决方法:在/usr/include/boost/config/compiler/gcc.hpp中注释掉# define BOOST_HAS_RVALUE_REFS
,如下所示。
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) && defined(__GXX_EXPERIMENTAL_CXX0X__)
// C++0x features are only enabled when -std=c++0x or -std=gnu++0x are
// passed on the command line, which in turn defines
// __GXX_EXPERIMENTAL_CXX0X__.
# define BOOST_HAS_DECLTYPE
//# define BOOST_HAS_RVALUE_REFS
# define BOOST_HAS_STATIC_ASSERT
# define BOOST_HAS_VARIADIC_TMPL
#else
我的代码现在编译成魔术:)谢谢@MvC。
答案 2 :(得分:0)
经过多次探索,按照@MvG的建议,我通过以下方式成功编译并链接了我的简单程序:
动态:
default: test
test.o: test.cpp
g++-4.5 -std=c++0x -I /home/son/boost_1_50/include/ -c test.cpp
test: test.o
g++-4.5 -std=c++0x -L /home/son/boost_1_50/lib/ test.o -lboost_thread -lboost_system -lboost_chrono -pthread -o test
run: test
LD_LIBRARY_PATH=/home/son/boost_1_50/lib/ ./test
静态:
default: test
test.o: test.cpp
g++-4.5 -std=c++0x -I /home/son/boost_1_50/include/ -c test.cpp
test: test.o
g++-4.5 -std=c++0x -L /home/son/boost_1_50/lib/ test.o -static -lboost_thread -lboost_system -lboost_chrono -pthread -o test
run: test
./test
test.cpp文件如下。
#include <boost/thread.hpp>
#include <stdio.h>
int main()
{
printf("boost thread tested by son\n");
boost::thread t;
return 0;
}
就是这样。我在使用CMake编译时遇到了困难,但这是另一个问题。