2
#include <boost/thread/thread.hpp>
#include <iostream>

using namespace std;

void hello() {
    cout << "Hello world, I'm a thread!" << endl;
}

int main(int argc, char* argv[]) {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!

    boost::thread thrd(&hello);
    thrd.join();
    return 0;
}

我确信上面的代码没有任何问题,因为我已经在 Microsoft Visual Studio Express Edition 上对其进行了测试。但它不能在 Eclipse 中编译。

我在 Windows 机器上执行此操作,我将 Eclipse Kepler 与 MinGW 工具链一起使用,我D:/tool/boost_1_54_0使用此命令在此目录中安装 boost ./b2 install --with-thread --libdir=stage/lib --includedir=stage/include --build-type=complete stage toolset=msvc。我在(项目属性> C/C++ 构建> 设置> MinGW C++ 链接器> 库)部分配置了额外的包含目录、库路径和线程库,即boost_thread-vc110-mt-1_54和?libboost_system-vc110-mt-1_54Libraries (-l)

这是编译的输出,你看到任何线索吗?

15:52:07 **** Incremental Build of configuration Debug for project Cpp2 ****
Info: Internal Builder is used for build
g++ "-LD:\\tool\\boost_1_54_0\\stage\\lib" -o Cpp2.exe "src\\Cpp2.o" -llibboost_thread-vc110-mt-1_54 -llibboost_system-vc110-mt-1_54 
src\Cpp2.o: In function `_static_initialization_and_destruction_0':
D:/tool/boost_1_54_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
D:/tool/boost_1_54_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
D:/tool/boost_1_54_0/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
src\Cpp2.o: In function `thread_exception':
D:/tool/boost_1_54_0/boost/thread/exceptions.hpp:51: undefined reference to `boost::system::system_category()'
src\Cpp2.o: In function `thread_data_base':
D:/tool/boost_1_54_0/boost/thread/win32/thread_data.hpp:123: undefined reference to `vtable for boost::detail::thread_data_base'
src\Cpp2.o: In function `ZN5boost6thread12start_threadEv':
D:/tool/boost_1_54_0/boost/thread/detail/thread.hpp:180: undefined reference to `boost::thread::start_thread_noexcept()'
src\Cpp2.o: In function `~thread':
D:/tool/boost_1_54_0/boost/thread/detail/thread.hpp:255: undefined reference to `boost::thread::detach()'
src\Cpp2.o: In function `ZN5boost6thread4joinEv':
D:/tool/boost_1_54_0/boost/thread/detail/thread.hpp:751: undefined reference to `boost::thread::get_id() const'
D:/tool/boost_1_54_0/boost/thread/detail/thread.hpp:751: undefined reference to `boost::this_thread::get_id()'
D:/tool/boost_1_54_0/boost/thread/detail/thread.hpp:756: undefined reference to `boost::thread::join_noexcept()'
src\Cpp2.o: In function `~thread_data':
D:/tool/boost_1_54_0/boost/thread/detail/thread.hpp:91: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
collect2: ld returned 1 exit status
4

2 回答 2

5

-llibboost_thread-vc110-mt-1_54

应该

-lboost_thread-vc110-mt-1_54

boost_1.45使用以下命令行选项为我工作

-IC:\MinGW\boost_1_45_0 -LC:\MinGW\boost_1_45_0\stage\lib -lboost_thread-mgw46-mt-1_45

于 2013-09-07T08:38:24.007 回答
2

首先运行bootstrap.bat mingw,然后b2 toolset=gcc在 boost_1_##_0/ 中运行。

一段时间后,您应该最终得到一个boost_1_##_0/stage/lib包含libboost_thread-mgw48-mt-1_55.a(或类似名称)的填充文件夹。

然后,您需要boost_1_##_0/stage/lib在项目属性中添加 MinGW C++ 链接器的库路径,并将boost_system-mgw48-mt-1_55&添加boost_thread-mgw48-mt-1_55为不带lib前缀和.a结尾的库。(顺序也可能很重要,所以如果不是,请尝试将系统放在首位)

于 2014-01-07T19:31:30.153 回答