2016-04-20 7 views
-2

#includeの行にブレークポイントを設定すると、gdbは行を無視してメインの次の命令で停止しますmain.cppはg++ -g -O2 -std=c++11です)。gdbは#includeディレクティブの行で停止しません

プログラムは完璧に動作します(-O2は結果に全く影響しません)。しかし、そのファイル内で何が何か正確に実行されているかを確認したいのですが、gdbでコードを入力できないためファイル。

他のファイル内でコードをデバッグするにはどうすればよいですか?それも可能ですか?

編集:ここではこれは、コードIでコード

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 
} 

includes.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 

このヘルプが欲しいです。

:デバッグ(正しい単語ではありません)によって、私は関数がどのように機能するかを調べることを意味しました。(関数私は完全に理解していない)。

+1

プリプロセッサディレクティブは、どのマシン命令にもマッピングされません。デバッガが何らかの方法でそれらを知覚すると思われるのはなぜですか?別のモジュールの関数をデバッグするには、この関数を呼び出します。 – Drop

+0

したがって、どのようにそのファイルの中にあるものをデバッグできますか? –

+1

ファイルをデバッグすることはできません。実行可能コードをデバッグすることができます。なぜ、関数の中に何かを最初に入れるのですか?これはナンセンスです。 – Drop

答えて

0

おそらく、MyFunction()でブレークした後、スタックを表示するために 'bt'コマンドを実行することができます。

includeは、コードを生成することはありませんプリプロセッサディレクティブです:そして、あなたが見る、フレームがソースファイルの観点から構成され、追加のスタックフレームまたは何スタックは、それがすべての

0

ファーストを助けるかもしれないがあります。デバッガは、実行可能なものに対してのみ停止することができます。コンパイル中にファイルをインクルードすると、実行時には機能しません。

次:

あなたの含まれるファイルは、いくつかの値、関数、クラス、および多く他を定義します。したがって、デバッガにどこで停止するかを知らせる必要があります。

そして、すべて: 「cpp」ファイルを含むことは本当にゴミです!これを行う理由はほとんどありません。

でもOK、続行する方法:あなたのヘッダファイル(または付属のcppファイル)が機能を提供する場合

、あなたは単にbreak Funcを行うと、あなたのプログラムを実行することができます。前にgdbのためにGUI内のファイルを開く必要はありません。

添付ファイルを調べたい場合は、list myheader.h:1も可能です。 1つは、ファイルを探し始めるコード行です。

ヒント:より詳細なヘルプを提供するために人がコンパイルできるコード例をご紹介ください。あなたは本当に分かりにくいです!

例セッション:

ヘッダー:FH

#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

関連する問題