0

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

文件.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。

Are there some known issues with gdb and C++11 (specifically libc++)? I know libc++ and libstdc++ can cause issues if used together, but I'm not trying to use them together (at least not consciously; all I want to use is libc++). Am I specifying some compilation options wrong? Is there a way to properly compile for C++11 on OS X and still be able to debug properly?

4

1 回答 1

1

GDB 6.3 is almost nine years old. That's just about eternity in Internet years. The product has improved greatly since then. Updating to the last stable release is a must for every developer.

于 2013-09-23T19:05:03.777 回答