How to debug using G++

closed account (EwCjE3v7)
Hello I would like to know the command for debugging the following code, I can debug with code::Blocks but not using the command which is g++ -D NDEBIG ......

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void print(vector<int> pv)
{
	for (auto i : pv) {
		cout << i << endl;
	}
	#ifndef NDEBUG
	cerr << "The size of the array is " << pv.size() << endl;
	#endif
}

int main()
{
	vector<int> v;
	int i;

	while (cin >> i) {
		v.push_back(i);
		print(v);
	}

	return 0;
}
This is a good tutorial on the subject:
http://cs.baylor.edu/~donahoo/tools/gdb/tutorial.html

You can use G++ with the -g option to generate/embed debug info in your binary. Then you use gdb to debug the code.

Specifically, run
1
2
$ gdb mybinary
(gdb) run


Then you have commands like: break, step, next which let you jump around the code and figure out what's what.
Last edited on
closed account (EwCjE3v7)
Sorry for my really late reply, I would like to thank you for your explanation and the link below. I will bookmark the link as it can come in handy, Thank you again
Topic archived. No new replies allowed.