How to automatically run the executable in GDB?

gdb -ex run ./a.out

If you need to pass arguments to a.out:

gdb -ex run --args ./a.out arg1 arg2 ...

EDIT:
Orion says this doesn’t work on Mac OSX.

The -ex flag has been available since GDB-6.4 (released in 2005), but OSX uses Apple’s fork of GDB, and the latest XCode for Leopard contains GDB 6.3.50-20050815 (Apple version gdb-967), so you are out of luck.

Building current GDB-7.0.1 release is one possible solution. Just be sure to read this.

Leave a Comment