0

boost::Qt Creator 2.5.2 でブースト ライブラリの一部を使用するプログラムをコンパイルしようとすると、名前空間内のさまざまなものに対して未定義の参照エラーが発生します。最初は、静的Boostライブラリと共有Qtライブラリを混在させていたためだと思ったので、link=shared runtime-link=sharedBuildオプションでBoostを再コンパイルしましたが、問題は残ります。次に、わずかに変更された Boost テスト プログラムを含む main.cpp だけで構成される非 Qt プレーン C++ プロジェクトを開始しました。

// 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...]

Qt Creator または mingw32-make を使用せずに、コマンド ラインから main.cpp をコンパイルすると、問題なく動作します。

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 (上記の各コンパイラ ビルド、静的および共有ライブラリを使用してソースからビルド)
  • MinGW 用の Qt Framework 4.8.3、プリコンパイル済みバイナリ。
  • Windows 用の Qt Creator 2.5.2。

Qmake の makecpecs 構成ファイルなどを確認しましたが、問題の根本を突き止めることができません。何か案は?

4

1 に答える 1

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

...手がかりになります。

トリックは、私のqmakeファイルに追加することであることが判明しましたCONFIG += exceptionsconsolecout/printfなどは実際に何でもしますが、それは本当に苦痛でした!)。ここで実際に何が起こっているのかわかりません。おそらくほとんどの Linux ディストリビューションは、qmake 仕様ファイルなどに何か特別なものを追加しています。

于 2013-07-23T02:15:39.907 に答える