2012-10-15 43 views
0

如果我嘗試編譯使用Qt Creator 2.5.2中的Boost庫的任何部分的程序,我會在boost::名稱空間中的各種事物中發現未定義的引用錯誤。起初,我認爲這是因爲我將靜態Boost庫與共享Qt庫混合在一起,所以我重新編譯了使用link=shared runtime-link=shared構建選項的Boost,但問題仍然存在。然後我就開始即由一個沒有任何非調質,純C++項目不止包含一個稍微修改加速測試程序的main.cpp:在Qt Creator中編譯提升依賴失敗的程序

// 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,使命令行中,給出了:

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-時許,工作得很好:

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 Libr aries 1.51.0(源自以上每個編譯器構建,靜態和共享庫)
  • Qt Framework 4.8.3適用於MinGW的預編譯二進制文件。
  • Qt Creator 2.5.2 for Windows。

我已經檢查過Qmake的makescpecs配置文件等等,但仍然找不出問題的根源。有任何想法嗎?

回答

2

這很可能爲時已晚,無法幫助您,但我今天只需要弄清楚這一點。我也使用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 

......它給出了一個線索。

訣竅竟然是添加CONFIG += exceptions(連同console所以COUT/printf的等等。其實做任何事情,這是一個真正的痛苦!)我的qmake的文件。我不知道這裏究竟發生了什麼,也許大多數Linux發行版爲qmake spec文件或其他東西添加了特別的東西。