6

I have the following toy program that gives errors with the MacPorts gcc on OSX 10.6

#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;
}

I have boost version 1.52 installed with MacPorts. I compile the program as

g++ a.cpp -lboost_program_options-mt -L/opt/local/lib -g -O0

It compiles fine:

$ ./a.out
Program continues

But it cannot print the help message:

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

I've heard that stuff like this can happen if the libraries are compiled with a different version of gcc than the one used to build the program. How do I check for this? I have

$ g++ --version
g++ (MacPorts gcc47 4.7.2_2) 4.7.2

Update: this seems to work on a Linux machine with and older Boost.

Update 2: the output of gdb follows

(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) 

Update 3: The program works fine with the Xcode gcc42, the problem only occurs with the MacPorts gcc.

highBandWidth
  • 16,751
  • 20
  • 84
  • 131
  • 3
    This code looks correct to me (compared with my sourcecode using program_options). Could you run this in gdb (compile with -ggdb3 -O0 and run `gdb --args ./a.out --help` and type `run` and `bt`)? – Rafał Rawicki Jan 16 '13 at 23:37
  • @RafałRawicki, just added the gdb info after compiling with your flags – highBandWidth Jan 17 '13 at 05:25

1 Answers1

1

The most likely reason for your error is that there is a mismatch between the interfaces presented in the program_options header files and the implementation as picked up in your compiled library. This could be either because you are accidentally picking up a compiled library from a different version of boost or perhaps because you compiled the library with different version of the compiler to the one you are using to compile your test program.

Bojan Nikolic
  • 1,296
  • 12
  • 8
  • Is there a way to check for example, which version of the compiled library it is, or which compiler was used to generate the library? – highBandWidth Feb 14 '13 at 13:38
  • On Linux you can use objdump -x to find out much of this information (SO name, glibc and cxxabi versions). Don't know about OS X though – Bojan Nikolic Feb 14 '13 at 16:40