1

我正在尝试执行以下从字符串中提取电子邮件地址的代码。我遇到分段错误。

// A.cpp
#include <boost/algorithm/string/trim.hpp>
#include <boost/regex.hpp>
#include<iostream>

int main()
{
   std::string rcptAddress("3J1eg==?= <dummyemail@edummyemail.com>");
   boost::regex exp("\\s", boost::regex::icase|boost::regex::perl);
   std::string fmt("");
   rcptAddress = boost::regex_replace(rcptAddress, exp, fmt);
   std::cout << rcptAddress << std::endl;
   exp.assign(".*<(.*)>.*");
   fmt = "$1";
   rcptAddress = boost::regex_replace(rcptAddress, exp, fmt);
   std::cout << rcptAddress << std::endl;
   return 0;
}

我使用以下开关编译了代码:

    g++ A.cpp  -g -O2 -Wall -Werror -fmessage-length=0 -fno-strict-aliasing -DBOOST_DISABLE_THREADS -finline-functions -L /root/.local/8.0.0-gcc4.1.2/lib/ -I /root/.local/8.0.0-gcc4.1.2/include/boost-1_39/ -lboost_regex-gcc41-mt-1_39

当我执行二进制文件时,我得到 SEGMENTATION FAULT 如下

LD_LIBRARY_PATH=/root/.local/8.0.0-gcc4.1.2/lib/ ./a.out
3J1eg==?=<dummyemail@edummyemail.com>
Segmentation fault

重新编译省略 -DBOOST_DISABLE_THREADS 的二进制文件给了我正确的结果。

g++ A.cpp  -g -O2 -Wall -Werror -fmessage-length=0 -fno-strict-aliasing -finline-functions -L /root/.local/8.0.0-gcc4.1.2/lib/ -I /root/.local/8.0.0-gcc4.1.2/include/boost-1_39/ -lboost_regex-gcc41-mt-1_39

我还尝试放入 -DBOOST_DISABLE_THREADS 并省略 -finline-functions 开关。执行这个二进制文件也给了我正确的结果。

g++ A.cpp  -g -O2 -Wall -Werror -fmessage-length=0 -fno-strict-aliasing -DBOOST_DISABLE_THREADS -L /root/.local/8.0.0-gcc4.1.2/lib/ -I /root/.local/8.0.0-gcc4.1.2/include/boost-1_39/ -lboost_regex-gcc41-mt-1_39

我无法找出为什么同时提供命令行开关 -DBOOST_DISABLE_THREADS 和 -finline-functions(在优化级别 -O3 中添加)会导致此处出现段错误。

任何帮助都会很棒!

我在 64 位 CentOS 5.8 上使用 g++ 4.1.2。Boost 库版本是 boost_1_39。

谢谢。

4

0 回答 0