Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

debugging templates with GDB

My gdb is GNU gdb Red Hat Linux (6.3.0.0-1.162.el4rh) and I can't debug templates. How can I debug templates with this debugger?

like image 259
Davit Siradeghyan Avatar asked Nov 03 '09 12:11

Davit Siradeghyan


People also ask

Can you use GDB with C++?

For C and C++ programs, gdb and ddd are debuggers that you can use.

What is GDB debugging tool?

GDB stands for the “Gnu DeBugger.” This is a powerful source-level debugging package that lets you see what is going on inside your program. You can step through the code, set breakpoints, examine and change variables, and so on. Like most Linux tools, GDB itself is command line driven, making it rather tedious to use.

Does Visual Studio have GDB?

Visual Studio Code supports the following debuggers for C/C++ depending on the operating system you are using: Linux: GDB. macOS: LLDB or GDB. Windows: the Visual Studio Windows Debugger or GDB (using Cygwin or MinGW)


1 Answers

if your problem is just about placing breakpoint in your code. Here is a little snippet

ex: main.cpp

#include <iostream>

template <typename T>
void coin(T v)
{
    std::cout << v << std::endl;
}

template<typename T>
class Foo
{
public:

    T bar(T c)
    {
        return c * 2;
    }
};

int main(int argc, char** argv)
{
    Foo<int> f;
    coin(f.bar(21));
}

compile with g++ -O0 -g main.cpp

gdb ./a.out
(gdb) b Foo<int>::bar(int)
Breakpoint 2 at 0x804871d: file main.cpp, line 16.
(gdb) b void coin<int>(int)
Breakpoint 1 at 0x804872a: file main.cpp, line 6.
(gdb) r
... debugging start

otherwise you could just use

(gdb) b main.cpp:16
like image 90
chub Avatar answered Sep 21 '22 15:09

chub