2017-03-10 43 views
1

我有gdb 7.6和g ++ 4.8.3的問題。我有以下3個文件在內部類GDB中的不完整類型與虛擬方法定義在一個單獨的文件

main.h

class A { 
public: 
    class B { 
    public: 
     virtual ~B(); 
     virtual void f(); 
     int abc; 
    }; 
}; 

b.cpp

#include "main.h" 

A::B::~B() {} 

void A::B::f() {} 

的main.cpp

#include "main.h" 

int main() 
{ 
    int a=0; 
    A::B x; 
    A::B *y = &x; 
    a = 10; 
    return a; 
} 

然後

>> g++ main.cpp b.cpp -o main -g 
>> gdb ./main 
GNU gdb (GDB) 7.6 
Copyright (C) 2013 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. Type "show copying" 
and "show warranty" for details. 
This GDB was configured as "x86_64-unknown-linux-gnu". 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>... 
Reading symbols from /tmp/gdb/main...done. 
(gdb) b 8 
Breakpoint 1 at 0x400654: file main.cpp, line 8. 
(gdb) r 
Starting program: /tmp/gdb/main 

Breakpoint 1, main() at main.cpp:8 
8  a = 10; 
(gdb) p *y 
$1 = <incomplete type> 

我們沒有問題,如果

  1. 這些方法的定義是在main.h.
  2. 該方法是虛擬的。

這是一個已知的問題嗎?如何解決該問題?

+0

也許嘗試使用鐺/ LLDB呢? – xaxxon

+0

相關問題:http://stackoverflow.com/q/29494132/72178 – ks1322

+0

鏈接建議-g。我在我的情況下使用-g,仍然有同樣的錯誤。 –

回答

1

看起來像這是gdb的bug。我也無法打印該變量用gdb 7.12,但LLDB調試器能夠打印:

$ lldb ./main 
Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
ImportError: No module named lldb.embedded_interpreter 
(lldb) target create "./main" 
Current executable set to './main' (x86_64). 
(lldb) b 8 
Breakpoint 1: where = main`main + 36 at main.cpp:8, address = 0x000000000040068a 
(lldb) r 
Process 9542 launched: './main' (x86_64) 
Process 9542 stopped 
* thread #1: tid = 9542, 0x000000000040068a main`main + 36 at main.cpp:8, name = 'main', stop reason = breakpoint 1.1 
    frame #0: 0x000000000040068a main`main + 36 at main.cpp:8 
    5  int a=0; 
    6  A::B x; 
    7  A::B *y = &x; 
-> 8  a = 10; 
    9  return a; 
    10 } 
(lldb) p *y 
(A::B) $0 = (abc = 0) 
+0

lldb與非LLVM編譯器一起工作嗎?我的代碼僅在linux中由gcc編譯。 –

+1

是的,我在這裏使用了gcc 6.2 + lldb。一般來說,你幾乎總是可以用gdb/lldb來使用gcc/clang,參見http://stackoverflow.com/q/21132194/72178。 – ks1322

+0

我在llvm 3.9.1中試過lldb。有用。 –

相關問題