简体   繁体   中英

How to compile boost thread using MinGW in Eclipse?

#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;
}

I am sure there is nothing wrong with the code above because I have tested it on Microsoft Visual Studio Express Edition. But it doesn't compile in Eclipse.

I'm doing this on Windows machine, I am using Eclipse Kepler with MinGW toolchain, I install boost in this directory D:/tool/boost_1_54_0 with this command ./b2 install --with-thread --libdir=stage/lib --includedir=stage/include --build-type=complete stage toolset=msvc . I have configure additional include directory, library path, and the thread library, which is boost_thread-vc110-mt-1_54 and libboost_system-vc110-mt-1_54 , in Libraries (-l) section (project properties > C/C++ build > Settings > MinGW C++ Linker > Libraries)?

This is the output of the compilation, do you see any clue on it?

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

-llibboost_thread-vc110-mt-1_54

should be

-lboost_thread-vc110-mt-1_54

I works for me with boost_1.45 with following command line options

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

First run bootstrap.bat mingw and then b2 toolset=gcc in boost_1_##_0/.

You should, after some time, end up with a populated boost_1_##_0/stage/lib folder containing libboost_thread-mgw48-mt-1_55.a (or similarly named).

You will want to then add boost_1_##_0/stage/lib as a library path for the MinGW C++ Linker in the project properties and add boost_system-mgw48-mt-1_55 & boost_thread-mgw48-mt-1_55 as libraries without the lib prefix and .a ending. (Order may also matter so try putting system first if it isn't)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM