包含boost / thread.cpp时的编译错误

时间:2012-07-30 21:47:14

标签: c++ multithreading boost boost-asio boost-thread

我有以下代码使用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,如果这些有任何帮助的话。提前谢谢。

3 个答案:

答案 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.3g++-4.5.4使用boost 1.49
  • 也可以正常运行

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

要手动安装提升,建议您按照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获取正确的标题
  4. linking您的申请
  5. 时包含${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的建议,我通过以下方式成功编译并链接了我的简单程序:

  • 手动构建并安装boost 1.50(使用MvG建议)
  • 使用以下2个make文件编译和链接源(1表示静态,1表示动态链接)。

动态:

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编译时遇到了困难,但这是另一个问题。