GDB不会停止与 #include 指令一致
gdb doesn't stop in a line with #include directive
问题是,当我在#include的行设置断点时,gdb会忽略该行,并在main中的下一条指令处停止(我用g++ -g -O2 -std=c++11
编译了main.cpp)。
这个程序运行得很好(-O2根本不会影响结果),但我想检查文件中到底是什么做了什么,但我做不到,因为gdb不允许我在文件中输入代码。
如何调试其他文件中的代码?这可能吗?
编辑:这是代码
main.cpp
#include <iostream>
#include <cstdlib>
#include <chrono>
#include "inc/includes.h"
template <class T>
void PrintVector(T* vector, int size){
for (int i=0; i<size; ++i){
std::cout << vector[i] << " ";
}
std::cout << std::endl;
}
template <class T>
void CheckTime(void (*f)(T*&, int), T* &vector, int size){
std::chrono::high_resolution_clock::time_point tantes, tdespues;
std::chrono::duration<double> transcurrido;
tantes = std::chrono::high_resolution_clock::now();
(*f)(vector, size);
tdespues = std::chrono::high_resolution_clock::now();
transcurrido = std::chrono::duration_cast<std::chrono::duration<double>(tdespues - tantes);
std::cout << size << " " << transcurrido.count() << std::endl;
}
int main(int argc, char * argv[]){
if (argc != 2){
std::cerr << "Formato " << argv[0] << " <num_elem>" << std::endl;
return -1;
}
int n = atoi(argv[1]);
int range;
#if defined RADIXSORTLSD || defined RADIXSORTMSD
unsigned short * array = new unsigned short[n];
range = (n<65536)?n:65536;
#else
unsigned int * array = new unsigned int[n];
range = n;
#endif
srand(time(0));
for (int i = 0; i < n; i++){
array[i] = rand()%range;
}
#ifdef PRINT
PrintVector(array, n);
#endif
#include "inc/select.h" //Here is the problem for debugging
#ifdef PRINT
PrintVector(array, n);
#endif
}
包括.h
#include "../src/radixsortlsd.cpp"
#include "../src/radixsortmsd.cpp"
#include "../src/mergesort.cpp"
#include "../src/bitonicsort.cpp"
#include "../src/insertion.cpp"
#include "../src/slowsort.cpp"
#include "../src/selection.cpp"
select.h这是我要调试的代码。我决定把它和主干分开,因为它会长很多。
// The calls to CheckTime takes the first parameter as the direction to a function, previously defined inside the cpps of includes.h
#ifdef RADIXSORTLSD
CheckTime(&RadixSortLSD, array, n);
#endif
#ifdef RADIXSORTMSD
CheckTime(&RadixSortMSD, array, n);
#endif
#ifdef MERGESORT
CheckTime(&MergeSort, array, n);
#endif
#ifdef INSERTION
CheckTime(&Insertion, array, n);
#endif
#ifdef SLOWSORT
CheckTime(&SlowSort, array, n);
#endif
#ifdef SELECTION
CheckTime(&Selection, array, n);
#endif
#ifdef BITONICSORT
CheckTime(&BitonicSort, array, n);
#endif
我希望这能有所帮助。注意,一切都编译得很好,工作也很好(我确保编译时定义的宏是正确的)
注意:我所说的调试(不是正确的词)是指检查函数的工作方式(我不完全理解这个函数)。
可能您可以中断MyFunction(),然后运行'bt'命令查看堆栈。然后,您会看到,是否有任何额外的堆栈帧,或者在源文件方面由哪些堆栈帧组成,这可能有助于
首先:
include
是一个从不生成代码的预处理器指令。调试器只能在可以执行的事情上停止。包含文件在编译期间有效,而不是在运行时。
下一篇:
您包含的文件将定义一些值、函数、类和许多其他内容。因此,您必须让调试器知道在哪里停止。
总之:包含"cpp"文件真是垃圾!这样做的理由很少。
但是好的,如何进行:
如果您的头文件(或包含的cpp文件)提供了一个函数,您只需执行break Func
并运行程序即可。以前无需在gdb的gui中打开任何文件。
如果你想查看包含的文件,你也可以list myheader.h:1
。1是您想要开始查看文件的代码行。
还有一个提示:请提供小得多的代码示例,人们可以自己编译,为您提供更详细的帮助。你的榜样真的很难理解!
示例会话:
标题:f.h
#include <stdlib.h>
void g(void)
{
malloc(4000);
}
void f(void)
{
malloc(2000);
}
main.cpp:
#include "f.h"
int main(void)
{
int i;
int* a[10];
for (i = 0; i < 10; i++) {
a[i] = (int*)malloc(1000);
}
f();
g();
for (i = 0; i < 10; i++) {
free(a[i]);
return 0;
}
}
示例会话:
> gdb prog
gdb) break f
Breakpoint 1 at 0x40061a: file main.cpp, line 10.
(gdb) run
Starting program: /home/xxx/go
Breakpoint 1, f () at f.h:10
10 malloc(2000);
(gdb) list
5 malloc(4000);
6 }
7
gdb )
现在,您可以使用step
遍历您的子例程。
- 每个 #include 指令的预处理器的时间成本是多少?
- "#include <path/to/header>"指令的含义(如"#include <loki/Functor.h>")
- 将#include指令包装到宏中
- #include 指令对SQL有意义吗?
- 在命名空间中放入一个标准的纯C头#include指令可以吗
- GDB不会停止与 #include 指令一致
- 来自 C 的指令 #include 和来自 C++ 的指令之间有什么区别吗?
- #include 指令中文件名的有效字符
- G++ 警告:#include 指令末尾的额外标记 [默认启用]
- 添加许多 -isystem include 指令是否会显著减慢编译过程
- C++中include指令和forward声明之间的区别
- 编写c++正则表达式以匹配#include预处理指令
- g++错误:预处理指令#INCLUDE无效
- #include 指令和"using"语句的顺序在C++头文件的开头是否重要?
- 正确的#include指令格式
- 如何在#include指令中使用宏
- 在 extern "C" { ... } 中包装标准 C++ 标头的 #include 指令?
- 用宏构造#include指令的路径
- 为什么更常见的#include指令没有默认包含?
- C和c++ #include指令中可以接受反斜杠吗?