首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有Boost依赖性的程序无法在Qt中编译

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

Stack Overflow用户
提问于 2012-10-15 14:11:56
回答 1查看 624关注 0票数 0

如果我试图编译QTCreator2.5.2中使用Boost库的任何部分的程序,就会得到boost::命名空间中各种内容的未定义引用错误。起初,我认为这是因为我将静态Boost库与共享Qt库混为一谈,所以我用link=shared runtime-link=shared build选项重新编译了Boost,但问题仍然存在。然后,我启动了一个非Qt的、简单的C++项目,该项目只包含一个包含稍微修改过的Boost测试程序的main.cpp:

代码语言:javascript
复制
// 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;
}

项目档案:

代码语言:javascript
复制
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中编译上面的项目,或者在命令行中使用mingw32-make提供如下信息:

代码语言:javascript
复制
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...]

在没有Qt或mingw32-make的情况下,从命令行编译main.cpp很好:

代码语言:javascript
复制
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-MinGW 32 (MinGW 4.6.1)
  • 我自己构建的MinGW:
    • MinGW 4.6.3与dwarf2异常处理
    • MinGW 4.6.3与SJLJ异常处理

  • Boost Libraries 1.51.0 (从源代码中构建上述编译器、静态库和共享库)
  • QtFramework4.8.3用于MinGW,预编译二进制文件。
  • 2.5.2适用于Windows。

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

EN

回答 1

Stack Overflow用户

发布于 2013-07-23 02:15:39

这很可能是太晚了,不能帮助你,但我今天只是想办法解决这个问题。我也在使用mingw32和Qt,并具有相同的未定义引用。

我首先根据另一个StackOverflow问题在“following”中添加以下内容来解决这个问题:

代码语言:javascript
复制
namespace boost
{
    void throw_exception(std::exception const &e) { assert(false); }
}

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

代码语言:javascript
复制
error: exception handling disabled, use -fexceptions to enable

...which给出了一个线索。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12897498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档