1

I can get more debug info if built my program on Windows compared to Linux. Here is my code:

#include <iostream>
#include <vector>

using namespace std;

class Base
{
public:
    Base() = default;
    virtual ~Base() = default;
};

class Derived : public Base
{
public:
    Derived() = default;
    ~Derived() = default;
private:
    int i = 1;
};

int main()
{
    vector<Base*> a;
    a.push_back(new Derived());
    return 0;
}

Build On Linux:Build On Linux

Build On Windows:enter image description here

It's obvious I can get more information with Windows build version. Such as vector info, vector element real type, derived object info... But Linux version, I only get the pointer address. By the way, they are all debugging by visual studio. Is there some way to add more debug info to the program built by GNU compiler? Such as compiler flags?

Rhysol
  • 481
  • 1
  • 3
  • 12
  • 1
    "Such as compiler flags?" -> https://stackoverflow.com/questions/12112338/get-the-compiler-options-from-a-compiled-executable – 463035818_is_not_an_ai Jul 27 '22 at 12:34
  • Add `-g` to include debug info in the binary built by GCC. No idea if this will bring you what you want tho – Yksisarvinen Jul 27 '22 at 12:36
  • @Yksisarvinen I already added -g option, but there is no different – Rhysol Jul 27 '22 at 12:38
  • 1
    What compiler flags are you currently using? – Eljay Jul 27 '22 at 12:38
  • @Eljay -O0 -ggdb3 -g -std=gnu++2a – Rhysol Jul 27 '22 at 12:40
  • Instead of `-O0` (which disables certain kinds of debug information), should use `-Og`. Instead of `-ggdb3 -g` just use `-ggdb3` (and if that doesn't work, then drop `-ggdb3` and just do `-g3`). – Eljay Jul 27 '22 at 12:46
  • 1
    What IDE are you using ? Did you try to use gdb directly to see if there is a problem with it ? ([official doc](https://sourceware.org/gdb/current/onlinedocs/gdb/), and [first tutorial I found](https://www.cs.umd.edu/~srhuang/teaching/cmsc212/gdb-tutorial-handout.pdf). – lemmel Jul 27 '22 at 12:48
  • @Eljay it doesn't work – Rhysol Jul 27 '22 at 13:24
  • @lemmel I am using Visual Studio, I also tried in gdb. And get the same result – Rhysol Jul 27 '22 at 13:25
  • @Rhysol when something does not behave as it should, I tend to think that I made an incorrect assumption; could you check if the binary is indeed built with debug symbols ? (with objdump ; see [that page on stack](https://stackoverflow.com/questions/3284112/how-to-check-if-program-was-compiled-with-debug-symbols#:~:text=To%20check%20if%20there's%20debug,a%20%22clean%22%20kernel%20object.) – lemmel Jul 27 '22 at 14:11
  • Is this really Visual Studio Community, Pro or Enterprise? Or are you using VSCode which is a completely different IDE and has a different tag at Stack Overflow. – drescherjm Jul 27 '22 at 14:48
  • 1
    @drescherjm Community version, not VSCode. Create a cmake project and use remote development. That would write code on windows, build and run on linux. So you can debug linux program by visual studio. – Rhysol Jul 29 '22 at 02:23

2 Answers2

1

I don't use visual studio, so not sure how you will make use of this answer from within that environment, however, I think you can get what you want using the GDB setting: set print object on.

To show this in action, here's my GDB (12.1) session, using the same test program that you posted:

$ gdb -q vec.x
Reading symbols from vec.x...
(gdb) b 26
Breakpoint 1 at 0x401245: file vec.cc, line 26.
(gdb) r
Starting program: /tmp/vec.x 

Breakpoint 1, main () at vec.cc:26
26      return 0;
(gdb) p a
$1 = std::vector of length 1, capacity 1 = {0x418eb0}
(gdb) p a[0]
$2 = (Base *) 0x418eb0
(gdb) p *a[0]
$3 = {
  _vptr.Base = 0x403040 <vtable for Derived+16>
}
(gdb) set print object on 
(gdb) p a[0]
$4 = (Derived *) 0x418eb0
(gdb) p *a[0]
$5 = (Derived) {
  <Base> = {
    _vptr.Base = 0x403040 <vtable for Derived+16>
  }, 
  members of Derived:
  i = 1
}
(gdb) q
Andrew
  • 3,770
  • 15
  • 22
  • You can create a cmake project in visual studio , and use remote development. The project will build and run on linux, but debug on windows by visual studio. My gdb version is 9.1 and can't directly print a[0]. – Rhysol Jul 29 '22 at 02:22
-1

I fixed this problem by add a init command to the ~/.gdbinit.

Add the following command to the first line of file ~/.gdbinit.

set print object on 
Rhysol
  • 481
  • 1
  • 3
  • 12
  • strange behaviour as it seems that is the default ([gdb doc](https://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_57.html). You should be wary with the settings of gdb and read the doc as there may be other problems that may raise. – lemmel Aug 02 '22 at 08:48