在未来的共享库加载时使断点挂起?(y 或 [n])

52c*_*der -3 c++ linux gdb

这是我的代码和我的操作,我想一步一步调试我的代码(下面的代码只是一个例子) main.cpp

#include <iostream>
#include <string>

extern int addd(int ,int);

int main()
{
    std::string str = "Hello";

    std::cout << str << std::endl;

    int a = 10,b = 20;

    std::cout << a + b << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

操作文件

int add(int a,int b)
{
    return a + b;
}
Run Code Online (Sandbox Code Playgroud)

我使用模板 makefile 表单makefiletemplate只是修改一些不常见的东西,当我发出 g++ 命令时:

[root@centos-linux-10 52coder]# make
g++ -std=c++11 -g   -O3 -Wall -Wextra      -c opr.cpp -o opr.o
g++ -std=c++11 -g   -O3 -Wall -Wextra      -c main.cpp -o main.o
g++ -std=c++11 -g   -O3 -Wall -Wextra         ./opr.o ./main.o  -Wl,--gc-sections -Wl,--strip-all   -o torun
Type ./torun to execute the program.
Run Code Online (Sandbox Code Playgroud)

我使用-O3 2 1,都出错了。我只是想一步一步地调试函数 main 中的代码,错误如下:

[root@centos-linux-10 52coder]# gdb torun 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.el7
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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/52coder/patchtool...(no debugging symbols found)...done.
(gdb) b main
Function "main" not defined.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) b /root/52coder.cpp:8
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) show directories
Source directories searched: $cdir:$cwd
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我我该怎么办这个问题,谢谢提前。

Bas*_*tch 5

您不能(轻松)调试剥离的可执行文件。因为 GDB 调试器需要其中的DWARF调试信息。

因此,只需将您的代码链接到:

 g++ -std=c++11 -g   -O3 -Wall -Wextra ./opr.o ./main.o  -o torun
Run Code Online (Sandbox Code Playgroud)

您可能会发现使用较少的编译器优化(例如-O0-Og-O1至多,而不是-O3)更容易调试程序。

附注。有一些方法可以将调试信息放在不同的文件中,但这是一个不同的问题(特定于 Linux)。