具有Boost依赖性的程序无法在Qt Creator中编译

时间:2012-10-15 14:11:57

标签: boost mingw qt-creator

如果我尝试在Qt Creator 2.5.2中编译使用Boost Libraries的任何部分的程序,我会在boost::命名空间中的各种事物中获得未定义的引用错误。起初我认为这是因为我将静态Boost库与共享Qt库混合在一起,因此我使用link=shared runtime-link=shared构建选项重新编译了Boost,但问题仍然存在。然后我启动了一个Non-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:
    • 使用dwarf2异常处理的MinGW 4.6.3
    • 使用SJLJ异常处理的MinGW 4.6.3
  • Boost Libraries 1.51.0(使用上述每个编译器构建的源代码构建,静态和共享库)
  • Qt Framework 4.8.3 for MinGW,预编译二进制文件。
  • 适用于Windows的Qt Creator 2.5.2。

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

1 个答案:

答案 0 :(得分:2)

这很可能为时已晚,无法帮助你,但我今天只需要解决这个问题。我也使用mingw32和Qt,并且具有相同的未定义引用。

我首先根据另一个StackOverflow问题在`some file&#39;中添加以下内容来解决它:

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

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

error: exception handling disabled, use -fexceptions to enable

......这给了一个线索。

诀窍原来是添加CONFIG += exceptions(和console一起,所以cout / printf等实际上做了什么,真是太痛苦了!)到我的qmake文件。我不知道这里到底发生了什么,也许大多数Linux发行版都为qmake规范文件添加了一些特殊内容。

相关问题