我有以下玩具程序,它在 OSX 10.6 上的 MacPorts gcc 出现错误
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
using namespace std;
int main(int ac, char* av[])
{
po::options_description desc("Allowed options");
desc.add_options() ("help", "produce help message") ;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 0;
}
cout << "Program continues\n";
return 0;
}
我安装了 MacPorts 的 boost 1.52 版。我将程序编译为
g++ a.cpp -lboost_program_options-mt -L/opt/local/lib -g -O0
它编译得很好:
$ ./a.out
Program continues
但它无法打印帮助信息:
$ ./a.out --help
Allowed options:
a.out(40110) malloc: *** error for object 0x7fff70ca3500: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap
我听说如果库是用不同版本的 gcc 编译的,而不是用于构建程序的版本,就会发生这样的事情。我该如何检查?我有
$ g++ --version
g++ (MacPorts gcc47 4.7.2_2) 4.7.2
更新:这似乎适用于具有较旧 Boost 的 Linux 机器。
更新 2:gdb 的输出如下
(gdb) run
Starting program: /Users/yasir/Downloads/mask.util/a.out --help
Reading symbols for shared libraries ++++.. done
Allowed options:
a.out(42256) malloc: *** error for object 0x7fff70ca3500: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal SIGABRT, Aborted.
0x00007fff821030b6 in __kill ()
(gdb) bt
#0 0x00007fff821030b6 in __kill ()
#1 0x00007fff821a39f6 in abort ()
#2 0x00007fff820bb195 in free ()
#3 0x00000001001188b4 in std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::overflow ()
(gdb)
更新 3:该程序在 Xcode gcc42 上运行良好,该问题仅在 MacPorts gcc 上出现。