How to setup gdb and Eclipse to debug C/C++ files on macOS
06/16/2019 Tags: IDEPurpose
For software development, Eclipse is one of the most common integrated development environments (IDE) that provides comprehensive facilities to computer programmers. IDEs contain the necessary compiler, interpreter, or both.
Besides, the GNU Debugger (GDB) is a debugger that works for many programming languages, including C, C++, and Objective-C and partially others. The GDB allows software developers to see what is going on inside another program while it executes and helps to catch bugs in the act.
Install Eclipse
The convenient and safe way to install Eclips is by downloading the installer file on the Eclipse Foundation website.
Setup GDB
Using brew to install gdb
The easiest way to install gdb is by using brew cmd to install. Please run this command:
$ brew install gdb
After running the command, you can find out your gdb version:
$ gdb --version
Generate a certificate by Keychain Access
If you try using GDB to debug a file, the console would show error since the Darwin kernel doesn’t allow gdb to control another process without having special rights. Thus, you must generate a certificate by Keychain Access to give gdb those permissions.
- Open and Launch Keychain Access application: Applications > Utilities > Keychain Access..
- Right-click on the System item and select Unlock Keychain “System”.
- Select Keychain Access > Certificate Assistant > Create a Certificate.
- Choose a name (e.g. gdb-cert).
- Set Identity Type to Self Signed Root.
- Set Certificate Type to Code Signing.
Finally, lock Keychain “System” and reboot your system.
Sign the certificate for GDB and Create a GDB command file
Open your Terminal prompt and run:
$ codesign -s gdb-cert < gdbPat >
,where gdb-cert is the name of your certificate and <gdbPath> is the full path to your gdb binary file.
Then, from the Terminal, you can do that by running this:
$ echo "set startup-with-shell off" >> ~/.gdbinit
Setep Eclipse for using GDB
There are two methods of configuring GDB. The first method is that configuring GDB for a specific project:
- Go to Run > Debug Configurations…
- Select a launch configuration (e.g. C/C++ Application)
- Open the Debugger tab from the menu
- Set GDB debugger to the full path of your gdb binary file (e.g. /usr/local/bin/gdb)
- Set GDB command file to the full path of your .gdbinit file: ~/.gdbinit
- Click on the Apply button
The second method is that defining a default configuration for GDB to be used in any Eclipse project:
- Go to Eclipse > Preferences
- Select C/C++ > Debug > GDB
- Set GDB debugger to the full path of your gdb binary file (e.g. /usr/local/bin/gdb)
- Set GDB command file to the full path of your .gdbinit file: ~/.gdbinit
- Click on the Apply button
NOTE: If you run debugger then it gets stuck on 100% process.. and also shows,
$ Launching : Configuring GDB Aborting configuring GDB
you can refer to [3] and find the solution by running as root:
$ sudo /Applications/Eclipse.app/Contents/MacOS/eclipse
Now, you can debug files from inside Eclipse using gdb. :)
Reference
[1] How to setup gdb and Eclipse to debug C++ files on macOS Sierra
[2] Eclipse 2019-03
[3] Launching gdb aborting configuring gdb issue
Thanks for reading! Feel free to leeve the comments below or email to me. Any pieces of advice or discussions are always welcome. :)