我正在尝试在使用qmake的qtcreator上,通过mingw(基于gcc4.6的TDM-mingw,32位)使用boost线程。我已经成功编译了boost 1.4.7。
bjam --toolset=gcc --layout=tagged --without-mpi --without-python -j 4 stage --build-type=complete
但是我无法让它链接起来。我尝试链接几个创建的libboost_thread库(libboost_thread.a、libboost_thread-mt.a、libboost_thread-mt-dll.a、libboost_thread-mt-s.a
),但它总是给我返回一个错误。
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;
}
qmake生成的编译指令如下所示:
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
根据这个问题,需要使用-DBOOST_THREAD_USE_LIB
编译,但是这样做只会导致
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()'
那么我该如何说服mingw链接boost_thread呢?(如果这是由于qmake给链接器提供的编译标志存在问题,那么我该如何说服它省略有问题的标志?)请注意不要删除HTML标签。
-enable-stdcall-fixup
,至少改为-Wl,--enable-stdcall-fixup
... - rubenvb<Qt>/mkspecs/win32-g++
)没有它(4.8git)。如果有,请在其前面添加-Wl,
(中间不要有空格)。 - rubenvb