使用GDB调试模板

Dav*_*yan 11 c++ linux templates gdb

我的gdb是GNU gdb Red Hat Linux(6.3.0.0-1.162.el4rh).我无法调试模板.如何使用此调试器调试模板?

chu*_*hub 11

如果您的问题只是在代码中放置断点.这是一个小片段

例如: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));
}
Run Code Online (Sandbox Code Playgroud)

用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
Run Code Online (Sandbox Code Playgroud)

否则你可以使用

(gdb) b main.cpp:16
Run Code Online (Sandbox Code Playgroud)