C++ boost::使用捕获时的regex segfaults
下面的简单程序有一个seg故障。这似乎与析构函数匹配结果有关C++ boost::使用捕获时的regex segfaults,c++,boost-regex,C++,Boost Regex,下面的简单程序有一个seg故障。这似乎与析构函数匹配结果有关 #include <iostream> #include <vector> #include <string> #include <boost/regex.hpp> using namespace std; int main(int argc, char *argv) { boost::regex re; boost::cmatch matches; boo
#include <iostream>
#include <vector>
#include <string>
#include <boost/regex.hpp>
using namespace std;
int main(int argc, char *argv)
{
boost::regex re;
boost::cmatch matches;
boost::regex_match("abc", matches, re.assign("(a)bc"));
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv)
{
boost::regex-re;
boost::cmatch匹配;
boost::regex_匹配(“abc”,匹配,重新分配(“a)bc”);
返回0;
}
编辑:我正在使用boost 1.39boost::regex是boost的少数几个组件之一,它不单独存在于头文件中……有一个库模块 您正在使用的库很可能是使用与您的应用程序不同的设置构建的 编辑:找到了一个示例场景,其中boost必须使用与应用程序相同的
-malign double
标志构建
这是boost库与应用程序不具有二进制兼容性的几种可能情况之一。您使用的是哪个版本的boost 我用boost 1.36编译了上面的示例,没有任何seg故障 如果您有多个boost库,请确保在运行时选择了正确的版本 Boost regex需要根据库编译
-lboost\u regex-gcc\u whatever-is-your-version
就我而言:
g++ -c -Wall -I /include/boost-1_36_0 -o main.o main.cpp
g++ -Wall -I /include/boost-1_36_0 -L/lib/boost-1_36_0 -lboost_regex-gcc33-mt main.o -o x
执行:
LD_LIBRARY_PATH=/lib/boost-1_36_0 ./x
您可以指向系统上boost include/libs的位置,注意库名中gcc和m(ulti)t(hread)的版本-这取决于您编译的内容,只需查看boost lib目录并从中选择一个版本的regex库。您使用的是临时变量,希望从中获取匹配项。我认为,您的问题将得到解决,如果您使用“abc”替代,您将使用以下内容:
string a("abc);
regex_match(a, matches, re.assign("(a)bc"));
我也有同样的问题。我尝试了Drew Dorman发布的解决方案,但没有成功。然后我发现我实际上是针对1.40链接的,但出于某种原因,标题是针对1.37的。一旦我下载了正确的头文件(1.40),它就停止了分段故障 当我使用调试符号
-g
编译并运行dbg回溯跟踪时,我注意到了这一点
希望这有帮助…在我的例子中,当从匹配结果中销毁m_subs向量时,它试图访问无效的内存位置