【问题标题】:Using Boost libraries with mingw将 Boost 库与 mingw 一起使用
【发布时间】:2011-12-10 01:26:13
【问题描述】:

我正在尝试使用 qmake 在 qtcreator 的 mingw(TDM-mingw,基于 gcc4.6 的 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'

我正在尝试编译的代码如下所示:

#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 

根据this,它必须用-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 给链接器的编译标志有问题,我如何说服它忽略有问题的标志?

【问题讨论】:

  • 您是否尝试过链接 boost_thread-mt-dll?
  • 你也应该去掉-enable-stdcall-fixup,至少改成-Wl,--enable-stdcall-fixup...
  • @rubenvb:你知道在 qmake 下怎么做吗?所有 -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -Wl,-subsystem,console -mthreads -Wl 似乎都已添加到某处qmake 内部
  • Grizzly:你使用的是什么版本的 Qt。我的 qmake.conf(位于&lt;Qt&gt;/mkspecs/win32-g++)没有它(4.8git)。如果它在那里,只需在它前面添加-Wl,(中间没有空格)。

标签: c++ boost mingw qmake


【解决方案1】:

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

【讨论】:

  • 不,没关系。仍然给出与问题最后部分中 OP 描述的相同的链接器错误...
  • 对不起,忘记我的第一条评论订单很重要!在我的特殊情况下,库引用的排序不是 main.o。我必须在 -lboost_thread 选项之前使用 boost_thread 指定我自己的库。
【解决方案2】:

我通过添加定义行修复了类似的错误:

#define BOOST_THREAD_USE_LIB

之前

#include <boost/thread.hpp>

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

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

【讨论】:

    【解决方案3】:

    迟到的答案:

    这里是关于如何使用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 个作业。)

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 我遇到了同样的问题,并且至少达到了您在问题中描述的程度。你还知道你是如何重新编译 boost 库的吗?可以在这里添加吗?
      • @g-makulik:抱歉,但不确定。当我这样做时,我做了很多实验/修补试图让它工作,我有/不知道为什么它突然工作了。我认为这可能与环境/系统变量有关。
      • 至少事实证明这是我的库正确排序的问题,请参阅我对 Dougs 回答的评论。无论如何,谢谢...
      猜你喜欢
      • 1970-01-01
      • 2011-05-27
      • 2011-01-23
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      相关资源
      最近更新 更多