<a id="use-gcc"></a>

# Develop with GCC on Ubuntu

This tutorial shows how to build and debug C programs on Ubuntu using GCC and GDB. For instructions on installing GCC and related tooling (including IDEs, debuggers, and the build system), see the dedicated guide on [How to set up a development environment for GCC on Ubuntu](https://documentation.ubuntu.com/ubuntu-for-developers/howto/gcc-setup.md#install-gcc). This article assumes that the tooling suggested in that article has been installed.

The GNU Compiler Collection (GCC) provides support for several programming languages. Working with the GCC toolchain in Ubuntu is straightforward, so this tutorial is limited to showing a basic ‘Hello, world!’ program in the C language and an introductory debugging session.

<a id="writing-a-sample-c-program"></a>

## Writing a sample C program

1. Create a project directory:
   `dev@ubuntu:~$ ``mkdir -p ~/c-projects/hello-world && cd ~/c-projects/hello-world`
2. Write a ‘Hello, world!’ program that includes setting a variable. Create a `hello.c` file with the following content:
   ```c
   #include <stdio.h>

   int main() {
       int number = 42;
       printf("Hello, world!\nThe answer to everything is %d.\n", number);
       return 0;
   }
   ```
3. Build the source code using the `gcc` compiler, specifying `hello` as the file name of the resulting executable:
   `dev@ubuntu:~/c-projects/hello-world$ ``gcc hello.c -o hello`

   This command incorporates the intermediate steps of preprocessing, compiling, and linking to generate an executable in one go. Use the `-v` option to display detailed information on how `gcc` invokes the individual tools that handle the intermediate steps.

   #### NOTE
   `gcc` can compile source code in many programming languages, including C, C++, and Assembly. The compiler guesses the language based on the file extension (for example, `.c`, `.cpp`, or `.s`). You can explicitly specify the language using the `-x` option as well.
4. Run the program executable:
   `dev@ubuntu:~/c-projects/hello-world$ ``Hello, world!
   > The answer to everything is 42.`

## Fixing simple bugs

The `gcc` compiler provides helpful output when encountering a problematic part of the source code.

1. Modify the ‘Hello, world!’ program source to omit an expected argument. Note the missing `number` argument on line 5:
   ```c
   #include <stdio.h>

   int main() {
       int number = 42;
       printf("Hello, world!\nThe answer to everything is %d.\n");
       return 0;
   }
   ```
2. Build the source:
   `dev@ubuntu:~/c-projects/hello-world$ ``gcc hello.c -o hello`
   ```text
   hello.c: In function ‘main’:
   hello.c:5:57: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
       5 |     printf("Hello, world!\nThe answer to everything is %d.\n");
       |                                                        ~^
       |                                                         |
       |                                                         int
   ```

   `gcc` displays a warning but still compiles the source code.
3. Run the program to see how it behaves without the integer argument specified:
   `dev@ubuntu:~/c-projects/hello-world$ ``./hello`
   ```text
   Hello, world!
   The answer to everything is -1350680728.
   ```

## Using the Make build system

Compiling manually by invoking `gcc` directly can help understand the build process. It is also adequate for simple projects like ‘Hello, world!’. A build system simplifies and automates the process.

See below for a simple example of using the GNU Make build system to build the ‘Hello, world!’ program from [Writing a sample C program](#writing-a-sample-c-program).

1. Create a basic Makefile (a file named `Makefile` in the project directory) with the following contents:
   ```makefile
   ## Variables:

   # Compiler
   CC = gcc

   # Target executable
   TARGET = hello

   # Source files
   SRCS = hello.c

   # Object files
   OBJS = $(SRCS:.c=.o)

   ## Targets:

   # Default target
   all: $(TARGET)

   # Linking
   $(TARGET): $(OBJS)
   	$(CC) -o $@ $^

   # Compiling
   %.o: %.c
   	$(CC) -c $< -o $@

   # Cleaning
   clean:
   	rm -f $(OBJS) $(TARGET)

   .PHONY: all clean
   ```

   The `Makefile` divides the build process into separate compilation and linking steps. It also uses “automatic variables” that allow for writing the compilation rules without specifying absolute file names. For an overview, see [Automatic Variables](https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html) in the GNU Make manual.

   #### IMPORTANT
   Makefile rules must start with the tab character. See [Rule Syntax](https://www.gnu.org/software/make/manual/html_node/Rule-Syntax.html).
2. Compile the program using `make`:
   `dev@ubuntu:~/c-projects/hello-world$ ``make`
   ```text
   gcc -c hello.c -o hello.o
   gcc -o hello hello.o
   ```

   Checking the resulting files would show:
   `dev@ubuntu:~/c-projects/hello-world$ ``ls -1`
   ```text
   hello
   hello.c
   hello.o
   Makefile
   ```
3. Run the program executable:
   `dev@ubuntu:~/c-projects/hello-world$ ``./hello`
   ```text
   Hello, world!
   The answer to everything is 42.
   ```

## Debugging with GDB

GDB is the standard debugger accompanying GNU GCC. While you can use the debugging capabilities of your preferred IDE, GDB offers adequate debugging support for the command line.

See the example below for a quick introduction to GDB use with the ‘Hello, world!’ program from [Writing a sample C program](#writing-a-sample-c-program).

1. Recompile the program with the `-g` option, which produces debugging information specifically for GDB:
   `dev@ubuntu:~/c-projects/hello-world$ ``gcc -g hello.c -o hello`
2. Run the debugger:
   `dev@ubuntu:~/c-projects/hello-world$ ``gdb hello`
   ```text
   #[snip]

   Reading symbols from hello...
   (gdb) start
   Temporary breakpoint 1 at 0x1155: file hello.c, line 4.
   Starting program: /home/rkratky/c-projects/hello-world/hello 

   [Thread debugging using libthread_db enabled]
   Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

   ./hello

   Temporary breakpoint 1, main () at hello.c:4
   4     int number = 42;
   (gdb) print number
   $1 = 32767
   (gdb) next
   5     printf("Hello, world!\nThe answer to everything is %d.\n", number);
   (gdb) print number
   $2 = 42
   (gdb)
   ```

   In the above example:
   1. Line 6: the `hello` program is started.
   2. Line 15: `number` is queried before being assigned.
   3. Line 17: program execution is stepped forward.
   4. Line 19: `number` is queried again after being assigned.
3. Press `Ctrl+X+A` to switch to the text user interface (TUI) of GDB for a more interactive experience:
   <div class="highlight-default notranslate"><div class="highlight">
   <span style="font-family:monospace"><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">┌─hello.c───────────────────────────────────────────────────────────┐</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#b2b2b2;">1 </span><span style="font-weight:bold;color:#5454ff;">#include</span><span style="color:#b2b2b2;"> </span><span style="color:#b21818;">&lt;stdio.h&gt;</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#b2b2b2;">2</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#b2b2b2;">3 </span><span style="color:#18b218;">int</span><span style="color:#b2b2b2;"> </span><span style="font-weight:bold;color:#ffffff;">main</span><span style="color:#b21818;">()</span><span style="color:#b2b2b2;"> </span><span style="color:#b21818;">{</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#b2b2b2;">4 &#160;&#160;&#160;&#160;</span><span style="color:#18b218;">int</span><span style="color:#b2b2b2;"> number </span><span style="color:#b21818;">=</span><span style="color:#b2b2b2;"> </span><span style="color:#b218b2;">42</span><span style="color:#b21818;">;</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&gt; &#160;&#160;&#160;&#160;5 </span><span style="color:#000000;background-color:#b2b2b2;">&#160;&#160;&#160;&#160;printf("Hello, world!\\nThe answer to everything is %d</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#b2b2b2;">6 &#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#5454ff;">return</span><span style="color:#b2b2b2;"> </span><span style="color:#b218b2;">0</span><span style="color:#b21818;">;</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#b2b2b2;">7 </span><span style="color:#b21818;">}</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#b2b2b2;">8</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;"> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">│</span><span style="color:#b2b2b2;">
   </span><br><span style="font-weight:bold;color:#686868;background-color:#18b2b2;">└───────────────────────────────────────────────────────────────────┘</span><span style="color:#b2b2b2;">
   </span><br><span style="color:#000000;background-color:#b2b2b2;">Thread 0x7ffff7f897 (src) In: main &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;L5 &#160;&#160;&#160;PC: 0x55555555515c </span><span style="color:#b2b2b2;">&#160;</span><br><span style="color:#b2b2b2;">(gdb) print number</span><span style="color:#b2b2b2;">
   </span><br><span style="color:#18b2b2;">$1</span><span style="color:#b2b2b2;"> = 32767</span><span style="color:#b2b2b2;">
   </span><br><span style="color:#b2b2b2;">(gdb) next</span><span style="color:#b2b2b2;">
   </span><br><span style="color:#b2b2b2;">(gdb) print number</span><span style="color:#b2b2b2;">
   </span><br><span style="color:#18b2b2;">$2</span><span style="color:#b2b2b2;"> = 42</span><span style="color:#b2b2b2;">
   </span><br><span style="color:#b2b2b2;">(gdb)</span><span style="color:#b2b2b2;">
   </span>
   <br></span></div></div>

   Type `help` at any point to see available commands.
4. Press `Ctrl+D` to quit GDB.
