使用mingw和Boost库

4

我正在尝试在使用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标签。

你尝试过链接boost_thread-mt-dll吗? - David Feurle
你还应该摆脱-enable-stdcall-fixup,至少改为-Wl,--enable-stdcall-fixup... - rubenvb
@rubenvb:你知道如何在qmake下实现这个吗?所有的-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -Wl,-subsystem,console -mthreads -Wl似乎已经被添加到了qmake的某个地方。 - Grizzly
1
Grizzly:你使用的Qt版本是什么?我的qmake.conf(位于<Qt>/mkspecs/win32-g++)没有它(4.8git)。如果有,请在其前面添加-Wl,(中间不要有空格)。 - rubenvb
4个回答

1
我通过添加定义行解决了类似的错误:
#define BOOST_THREAD_USE_LIB

之前

#include <boost/thread.hpp>

这显然使链接器将库libboost_thread-mt.a作为静态库使用(应该如此),而不是尝试动态链接或其他方式。

建议参考这里:Code Blocks, MinGW, Boost, and static linking issues


1
晚回答:

以下是如何使用MinGW自行编译boost的完整说明

...然后还有如何配置Eclipse以找到头文件和库。虽然不是通过qtcreator,但应该也可以工作。

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

总之,使用类似以下的方式进行编译:
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

(variant=...行应该与.\b2在同一行。选项-j3仅用于并行运行3个作业。)


1

我认为你需要在main.o之前列出boost_thread -- 顺序很重要。


不,那没关系。仍然会出现与问题描述者在问题的最后部分所描述的相同的链接器错误... - πάντα ῥεῖ
抱歉,忘记了我的第一条评论,顺序很重要!对于我的特殊情况,库引用的顺序不是main.o。我必须在-lboost_thread选项之前指定自己的库,使用boost_thread。 - πάντα ῥεῖ

0

刚刚我又重新编译了boost,现在它可以工作了,所以我认为这只是bjam出于某种原因使用了错误的mingw版本的问题。


我有同样的问题,并且至少已经像你在问题中描述的那样解决了一部分。你还记得你是如何重新编译boost库的吗?能否在这里添加一下呢? - πάντα ῥεῖ
@g-makulik:抱歉,我不能确定。当我尝试做这个的时候,我已经进行了很多实验和调试,试图让它工作,所以我不知道为什么突然间它可以工作了。我认为这可能与环境/系统变量有关。 - Grizzly
最终问题是我的库的正确排序问题,详见我在道格答案下的评论。无论如何,还是谢谢您... - πάντα ῥεῖ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接