gpt4 book ai didi

c++ - 包含 boost/thread.cpp 时出现编译错误

转载 作者:行者123 更新时间:2023-11-30 02:03:33 24 4
gpt4 key购买 nike

我有以下代码使用 Boost ASIO 设置 TCP 客户端。这是我的代码改编自 Boost 文档的 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 (*)()]’

如果我删除对 boost/thread.hpp 的包含,并通过对 io_service.run() 的简单调用替换对 t 的声明,它将编译并工作;我想知道这个编译错误是否与 boost 版本有关。如果有任何帮助,我正在使用 Boost ASIO 1.42、Ubuntu 11.04 和 Eclipse。提前谢谢你。

最佳答案

我写了一个包含单个 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.3g++-4.5.4 使用 boost 1.49 也能正常工作

所以我真的建议您为此使用更新版本的 boost。您不必在系统范围内安装它,而是可以为单个用户安装它。所以你不依赖于 ubuntu 包。

要手动安装boost,我建议你按照Getting Started on Unix Variants指南:

  1. 解压boost_1_50_0.tar.bz2到一些临时源目录和 cd 进入那个
  2. Build and install使用 ./bootstrap.sh --prefix=${HOME}/boost_1_50 && ./b2 install
  3. Compile您的应用程序使用 -I ${HOME}/boost_1_50 获取正确的 header
  4. linking 时将 ${HOME}/boost_1_50/lib/libboost_thread.a 作为参数包含在内你的申请

使用静态 libboost_thread.a 而不是动态 libbtoost_thread.so 将确保您不必担心定位库来启动您的应用程序。您需要的来自 boost 的所有内容都将包含在您的主二进制文件中。

关于c++ - 包含 boost/thread.cpp 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11730150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com