How to compile C++ code in GDB?

Viewed 1606

I'm following this manual:

https://sourceware.org/gdb/onlinedocs/gdb/Compiling-and-Injecting-Code.html

$ gdb ./test
(gdb) break main
(gdb) run
(gdb) compile code std::cout << "Hello world\n";
No compiler support for language c++.
(gdb) compile code print("hello world")
No compiler support for language c++.

Does this mean that g++ is not supported? Or I need to configure GDB in some special way?

1 Answers

This is very likely that the debugger you are using is too old. It sounds too me that prior to 7.12 (maybe even later), user cannot redefine the compile-gcc symbol. As a result, there seems to be no way to specify a compiler for C++.. Nevertheless, the issue has been reported and addressed. I guess the best solution is to update gdb to a newer version, by compiling it from the official repository.

Another solution would be to pre-compile the code manually and inject it manually within gdb, as explained here. Sounds fun but not sure this is the easy way!

Related