Louis Better than before

How to setup gdb and Eclipse to debug C/C++ files on macOS

Purpose

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.

  1. Open and Launch Keychain Access application: Applications > Utilities > Keychain Access..
  2. Right-click on the System item and select Unlock Keychain “System”.
  3. Select Keychain Access > Certificate Assistant > Create a Certificate.
  4. Choose a name (e.g. gdb-cert).
  5. Set Identity Type to Self Signed Root.
  6. 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:

  1. Go to Run > Debug Configurations…
  2. Select a launch configuration (e.g. C/C++ Application)
  3. Open the Debugger tab from the menu
  4. Set GDB debugger to the full path of your gdb binary file (e.g. /usr/local/bin/gdb)
  5. Set GDB command file to the full path of your .gdbinit file: ~/.gdbinit
  6. Click on the Apply button

The second method is that defining a default configuration for GDB to be used in any Eclipse project:

  1. Go to Eclipse > Preferences
  2. Select C/C++ > Debug > GDB
  3. Set GDB debugger to the full path of your gdb binary file (e.g. /usr/local/bin/gdb)
  4. Set GDB command file to the full path of your .gdbinit file: ~/.gdbinit
  5. 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. :)