使用libc ++会导致GDB在OS X上发生段错误

时间:2013-09-23 15:07:02

标签: c++ macos c++11 gdb libc++

我正在尝试将C ++ 11(在OS X上使用Clang和libc ++)用于程序,但每当我使用gdb进行调试并尝试检查标准容器时,gdb段错误。这是一个最小的例子:

file.cpp:

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
    std::string str = "Hello world";

    std::cout << str << std::endl; // Breakpoint here
}

如果我使用以下代码编译C ++ 11:

$ c++ --version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix
$
$ c++ -ggdb -std=c++11 -stdlib=libc++ -Wall -pedantic -O0 -c file.cpp
$ c++ -ggdb -std=c++11 -stdlib=libc++ -Wall -pedantic -O0 file.o -o program

然后调试如下,当我尝试p str.size()时崩溃:

$ gdb program
GNU gdb 6.3.50-20050815 (Apple version gdb-1824) (Wed Feb  6 22:51:23 UTC 2013)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries ... done

(gdb) br file.cpp:8
Breakpoint 1 at 0x100000d80: file file.cpp, line 8.
(gdb) run
Starting program: /Users/mjbshaw/School/cs6640/2/program 
Reading symbols for shared libraries ++............................. done

Breakpoint 1, main (argc=1, argv=0x7fff5fbffab0) at file.cpp:8
8       std::cout << str << std::endl; // Breakpoint here
(gdb) p str.size()

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
std::__1::operator<< <char, std::__1::char_traits<char>, std::__1::allocator<char> > (__os=@0x7fff5fc3d628, __str=@0x1) at string:1243
1243    
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on"
Evaluation of the expression containing the function (std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__is_long() const) will be abandoned.

如果我不在gdb中运行它,我没有崩溃,它工作正常(但我需要gdb来调试我的程序)。另外,如果我从编译选项中删除-std=c++11 -stdlib=libc++,那么它工作正常(即使在gdb中),但我的程序需要C ++ 11。

gdb和C ++ 11(特别是libc ++)是否存在一些已知问题?我知道如果一起使用libc ++和libstdc ++可能会引起问题,但我并不是想一起使用它们(至少不是有意识地使用它们;我想要使用的只是libc ++)。我指定一些编译选项错了吗?有没有办法在OS X上正确编译C ++ 11并且仍能正常调试?

1 个答案:

答案 0 :(得分:1)

GDB 6.3差不多九年了。这在互联网时代几乎是永恒的。从那时起,产品有了很大的改进。每个开发人员都必须更新到上一个稳定版本。