【问题标题】:Programs with Boost Dependency Fail to Compile in Qt Creator具有 Boost 依赖关系的程序无法在 Qt Creator 中编译
【发布时间】:2012-10-15 14:11:56
【问题描述】:

如果我尝试在 Qt Creator 2.5.2 中编译使用 Boost Libraries 的任何部分的程序,则会收到对 boost:: 命名空间中各种事物的未定义引用错误。起初我以为是因为我将静态 Boost 库与共享 Qt 库混合在一起,所以我使用 link=shared runtime-link=shared 构建选项重新编译了 Boost,但问题仍然存在。然后我开始了一个非 Qt、Plain C++ 项目,它只包含一个 main.cpp,其中包含一个稍微修改过的 Boost 测试程序:

// main.cpp, cin-less version of 
//     http://www.boost.org/doc/libs/1_51_0/more/getting_started/windows.html#test-your-program

#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main() {

    std::string headerLines = "To: George Shmidlap\n" \
                              "From: Rita Marlowe\n" \
                              "Subject: Will Success Spoil Rock Hunter?\n" \
                              "---\n" \
                              "See subject.\n";

    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
    boost::smatch matches;

    std::string::iterator newLinePos = std::find(headerLines.begin(), headerLines.end(), '\n');
    std::string::iterator startPos = headerLines.begin();

    while(newLinePos != headerLines.end()) {
        if (boost::regex_match(std::string(startPos, newLinePos++), matches, pat)) {
            std::cout << "\nRegex Match: " << matches[2];
        }

        startPos = newLinePos;
        newLinePos = std::find(startPos, headerLines.end(), '\n');
    }

    char temp[3];
    std::cin.getline(temp, 2);
    return 0;
}

项目文件:

TEMPLATE = app
CONFIG += console
CONFIG -= qt

SOURCES += main.cpp

Debug {
    LIBS += -lboost_regex-mgw46-mt-d-1_51
}

release {
    LIBS += -lboost_regex-mgw46-mt-1_51
}

从 Qt Creator 中编译上述项目,或在命令行中使用 mingw32-make,给出:

E:\BoostTest-483-MinGW_Debug\debug\main.o:-1: In function `ZN5boost13match_resultsIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEE17raise_logic_errorEv':

c:\tdm-mingw32\include\boost\regex\v4\match_results.hpp:562: error: undefined reference to `boost::throw_exception(std::exception const&)'

E:\BoostTest-483-MinGW_Debug\debug\main.o:-1: In function `ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE14construct_initERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsE':

c:\tdm-mingw32\include\boost\regex\v4\perl_matcher_common.hpp:55: error: undefined reference to `boost::throw_exception(std::exception const&)'

[etc...]

从命令行编译 main.cpp,无需 Qt Creator 或 mingw32-make,工作正常:

E:\BoostTest>g++ -s -O3 main.cpp -o main.exe -lboost_regex-mgw46-mt-1_51

E:\BoostTest>main.exe

Regex Match: Will Success Spoil Rock Hunter?

E:\BoostTest>g++ -s -O3 main.cpp -o main-dbg.exe -lboost_regex-mgw46-mt-d-1_51

E:\BoostTest>main-dbg.exe

Regex Match: Will Success Spoil Rock Hunter?

测试:

  • TDM-MinGW32 (MinGW 4.6.1)
  • 我自己构建的 MinGW:
    • MinGW 4.6.3 处理 dwarf2 异常
    • MinGW 4.6.3 与 SJLJ 异常处理
  • Boost Libraries 1.51.0(使用上述每个编译器版本、静态库和共享库从源代码构建)
  • 用于 MinGW 的 Qt Framework 4.8.3,预编译的二进制文件。
  • Qt Creator 2.5.2 for Windows。

我已经检查了 Qmake 的 makecpecs 配置文件等,但仍然无法找出问题的根源。有什么想法吗?

【问题讨论】:

    标签: boost mingw qt-creator


    【解决方案1】:

    这很可能为时已晚,无法为您提供帮助,但我今天必须解决这个问题。我也在使用 mingw32 和 Qt 并且有相同的未定义引用。

    根据另一个 StackOverflow 问题,我首先通过在“某些文件”中添加以下内容来解决它:

    namespace boost
    {
        void throw_exception(std::exception const &e) { assert(false); }
    }
    

    但是我实际上想抛出一个异常,所以将assert(false); 更改为throw e;。这立即引发了编译错误:

    error: exception handling disabled, use -fexceptions to enable
    

    ...提供线索。

    原来的诀窍是在我的 qmake 文件中添加 CONFIG += exceptions(以及 console 所以 cout/printf 等实际上可以做任何事情,这真的很痛苦!)。我不知道这里到底发生了什么,也许大多数 Linux 发行版都在 qmake 规范文件中添加了一些特殊的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 2012-04-10
      • 1970-01-01
      相关资源
      最近更新 更多