Grizzly
Grizzly

Reputation: 20191

Using Boost libraries with mingw

I'm tryin to use boost threads on mingw (TDM-mingw, 32bit based on gcc4.6) from qtcreator using qmake. I managed to compile boost 1.4.7 using

bjam --toolset=gcc --layout=tagged  --without-mpi --without-python -j 4 stage --build-type=complete

However I simply can not get it to link. I tried to link against several of the libboost_thread libraries created (libboost_thread.a, libboost_thread-mt.a, libboost_thread-mt-dll.a, libboost_thread-mt-s.a), however it always ends up giving me

ld.exe: warning: cannot find entry symbol nable-stdcall-fixup; defaulting to 00401000
main.o:main.cpp:(.text.startup+0x76): undefined reference to `_imp___ZN5boost6thread12start_threadEv'
main.o:main.cpp:(.text.startup+0x89): undefined reference to `_imp___ZN5boost6thread4joinEv'
main.o:main.cpp:(.text.startup+0x9c): undefined reference to `_imp___ZN5boost6threadD1Ev'
main.o:main.cpp:(.text.startup+0xdb): undefined reference to `_imp___ZN5boost6threadD1Ev'

The code I'm trying to compile looks like this:

#include <boost/thread.hpp>
struct thread_main
{ void operator()(){ std::cout<<"Hello World"<<std::endl; } };

int main(int argc, char* argv[])
{
   boost::thread thread((thread_main()));
   thread.join();
   return 0;
}

The compile instructions generated by qmake are as followed:

 g++ -c -std=gnu++0x -fopenmp -march=i686 -mtune=generic -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I'e:/Qt/4.73/Desktop/Qt/4.7.3/mingw/include' -I'e:/Qt/4.73/Desktop/Qt/4.7.3/mingw/include/ActiveQt' -I'release' -I'../Test' -I'.' -I'e:/Qt/4.73/Desktop/Qt/4.7.3/mingw/mkspecs/win32-g++' -o main.o ../Test/main.cpp
 g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -Wl,-subsystem,console -mthreads -Wl -o Test.exe.exe main.o  -L'e:/boost/stage/lib' -L'e:/Qt/4.73/Desktop/Qt/4.7.3/mingw/lib' -fopenmp -l boost_thread 

According to this it has to be compiled with -DBOOST_THREAD_USE_LIB, however doing so only leads to

ld.exe: warning: cannot find entry symbol nable-stdcall-fixup; defaulting to 00401000
main.o:main.cpp:(.text.startup+0x75): undefined reference to `boost::thread::start_thread()'
main.o:main.cpp:(.text.startup+0x87): undefined reference to `boost::thread::join()'
main.o:main.cpp:(.text.startup+0x99): undefined reference to `boost::thread::~thread()'
main.o:main.cpp:(.text.startup+0xd7): undefined reference to `boost::thread::~thread()'

So how can I convice mingw to link against boost_thread (or if it's a problem with the compile flags given to the linker by qmake, how do I convice it to omit problematic flags?

Upvotes: 4

Views: 4022

Answers (4)

Johan Lundberg
Johan Lundberg

Reputation: 27038

Late answer:

Here's a full description on how to compile boost yourself with MinGW

...and then also how to configure Eclipse to find the headers and libraries. It's not via qtcreator but should work as well.

http://scrupulousabstractions.tumblr.com/post/37052323632/boost-mingw-eclipse

In summary, compile using something like this:

cd F:\coding\boost_1_52_0
.\bootstrap.bat
.\b2 --prefix=F:\coding\some_installation_location toolset=gcc 
       variant=debug,release link=static,shared threading=multi install -j3

(the variant= ... line is meant to be on the same line as .\b2. The option -j3 is just for running 3 jobs in parallel.)

Upvotes: 1

Bartel
Bartel

Reputation: 11

I got a similar error fixed by adding the define line:

#define BOOST_THREAD_USE_LIB

before

#include <boost/thread.hpp>

which apparently makes the linker use the library libboost_thread-mt.a as a static library (as should) and not trying to link it dynamically or such.

as suggested here: Code Blocks, MinGW, Boost, and static linking issues

Upvotes: 1

Grizzly
Grizzly

Reputation: 20191

Just now I recompiled boost once again and now it works, so I assume it was just a case of bjam using the wrong mingw version for some reason

Upvotes: 0

Doug Moscrop
Doug Moscrop

Reputation: 4544

I think you need to list boost_thread before main.o -- the order is important.

Upvotes: 1

Related Questions