如果我试图编译QTCreator2.5.2中使用Boost库的任何部分的程序,就会得到boost::命名空间中各种内容的未定义引用错误。起初,我认为这是因为我将静态Boost库与共享Qt库混为一谈,所以我用link=shared runtime-link=shared build选项重新编译了Boost,但问题仍然存在。然后,我启动了一个非Qt的、简单的C++项目,该项目只包含一个包含稍微修改过的Boost测试程序的main.cpp:
// 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中编译上面的项目,或者在命令行中使用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或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?经以下测试:
我检查了Qmake的makescpecs配置文件等,但仍然找不出问题的根源。有什么想法吗?
发布于 2013-07-23 02:15:39
这很可能是太晚了,不能帮助你,但我今天只是想办法解决这个问题。我也在使用mingw32和Qt,并具有相同的未定义引用。
我首先根据另一个StackOverflow问题在“following”中添加以下内容来解决这个问题:
namespace boost
{
void throw_exception(std::exception const &e) { assert(false); }
}但是,实际上我想抛出一个异常,因此将assert(false);更改为throw e;。立即抛出编译错误:
error: exception handling disabled, use -fexceptions to enable...which给出了一个线索。
其诀窍是添加CONFIG += exceptions (以及console,所以cout/printf等实际上做了任何事情,这是一个真正的痛苦!)我的qmake文件。我不知道这里到底发生了什么,也许大多数Linux发行版都为qmake文件添加了一些特殊内容。
https://stackoverflow.com/questions/12897498
复制相似问题