> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/all-about-c/makefile/coloring.md).

# Coloring

Makefiles themselves don't have built-in support for adding color or text styling. However, you can achieve colorized output by using **ANSI escape codes**, which are a standard for controlling text formatting (including colors) in terminal output.

Let’s break it down step by step!

***

#### 1. **ANSI Escape Codes Overview**

ANSI escape codes are special character sequences that modify the appearance of text printed to the terminal. These codes work in most modern terminals (Linux, macOS, and Windows terminals like PowerShell).

**Basic Structure of an ANSI Escape Code:**

* **Escape Character**: `\033` or `\x1b`
* **Opening Sequence**: `[` (the opening bracket)
* **Code**: A number (or numbers) representing a style or color.
* **Ending Sequence**: `m` (this marks the end of the code)

For example:

* **`\033[31m`**: Sets the text color to **red**.
* **`\033[0m`**: Resets the text formatting to default.

#### 2. **Common ANSI Escape Codes for Text Styling**

Here are some common codes for changing text color and style:

* **Text Colors**:
  * `30` = Black
  * `31` = Red
  * `32` = Green
  * `33` = Yellow
  * `34` = Blue
  * `35` = Magenta
  * `36` = Cyan
  * `37` = White
* **Background Colors** (adding 10 to the number):
  * `40` = Black background
  * `41` = Red background
  * `42` = Green background
  * `43` = Yellow background
  * `44` = Blue background
  * `45` = Magenta background
  * `46` = Cyan background
  * `47` = White background
* **Text Styles**:
  * `1` = Bold
  * `4` = Underline
  * `7` = Inverse (swap foreground and background)

#### 3. **Using Colors in Makefile**

To use these escape codes in your Makefile, you can define variables for the colors and apply them to your echo statements. Let’s look at a few examples!

**Example: Defining Color Variables**

You can define color variables in your Makefile for reusability:

```makefile
# Define color variables
RED = \033[31m
GREEN = \033[32m
YELLOW = \033[33m
BLUE = \033[34m
RESET = \033[0m

CC = gcc
CFLAGS = -Wall

# Target to build the main program
main: main.o utils.o
    @echo "$(BLUE)Linking program...$(RESET)"
    $(CC) $(CFLAGS) -o $@ $^
    @if [ $$? -eq 0 ]; then echo "$(GREEN)Compilation and linking successful!$(RESET)"; else echo "$(RED)Linking failed!$(RESET)"; exit 1; fi

# Rule to compile .c files into .o files
%.o: %.c
    @echo "$(YELLOW)Compiling $<...$(RESET)"
    $(CC) $(CFLAGS) -c $< -o $@
    @if [ $$? -eq 0 ]; then echo "$(GREEN)Successfully compiled $@$(RESET)"; else echo "$(RED)Error compiling $@$(RESET)"; exit 1; fi

# Clean target to remove generated files
clean:
    @echo "$(BLUE)Cleaning up...$(RESET)"
    rm -f *.o main
    @echo "$(GREEN)Cleanup complete.$(RESET)"
```

**Explanation:**

* **Color Variables**: `RED`, `GREEN`, `YELLOW`, `BLUE`, and `RESET` are variables that store the ANSI escape codes for colors and reset.
* **Apply Colors**: In the `echo` statements, we use `$(COLOR_NAME)` to apply the color, followed by `$(RESET)` to reset to the default text style after the message.

***

#### 4. **Text Styling with Colors**

You can combine both colors and styles to make your messages more noticeable.

For example:

```makefile
BOLD = \033[1m
UNDERLINE = \033[4m
RESET = \033[0m

CC = gcc
CFLAGS = -Wall

# Build the main program
main: main.o utils.o
    @echo "$(BOLD)Compiling program...$(RESET)"
    $(CC) $(CFLAGS) -o $@ $^
    @if [ $$? -eq 0 ]; then echo "$(GREEN)Compilation and linking successful!$(RESET)"; else echo "$(RED)Linking failed!$(RESET)"; exit 1; fi

# Compile .c files into .o files
%.o: %.c
    @echo "$(YELLOW)Compiling $<...$(RESET)"
    $(CC) $(CFLAGS) -c $< -o $@
    @if [ $$? -eq 0 ]; then echo "$(GREEN)Successfully compiled $@$(RESET)"; else echo "$(RED)Error compiling $@$(RESET)"; exit 1; fi

# Clean target
clean:
    @echo "$(UNDERLINE)Cleaning up...$(RESET)"
    rm -f *.o main
    @echo "$(GREEN)Cleanup complete.$(RESET)"
```

This makes the compilation and clean-up messages stand out, with bold, underlined, and colored text.

***

#### 5. **Advanced Example with Multiple Styles**

You can even mix background colors, bold text, and more for full control over your output styling.

```makefile
BOLD = \033[1m
UNDERLINE = \033[4m
RED = \033[31m
GREEN = \033[32m
YELLOW = \033[33m
BLUE = \033[34m
BG_RED = \033[41m  # Red background
BG_GREEN = \033[42m # Green background
RESET = \033[0m

CC = gcc
CFLAGS = -Wall

# Main target
main: main.o utils.o
    @echo "$(BOLD)$(BG_BLUE)Compiling the main program...$(RESET)"
    $(CC) $(CFLAGS) -o $@ $^
    @if [ $$? -eq 0 ]; then echo "$(GREEN)Compilation and linking successful!$(RESET)"; else echo "$(RED)Linking failed!$(RESET)"; exit 1; fi

# Compile .c files into .o files
%.o: %.c
    @echo "$(YELLOW)Compiling $<...$(RESET)"
    $(CC) $(CFLAGS) -c $< -o $@
    @if [ $$? -eq 0 ]; then echo "$(GREEN)Successfully compiled $@$(RESET)"; else echo "$(RED)Error compiling $@$(RESET)"; exit 1; fi

# Clean target
clean:
    @echo "$(BG_RED)Cleaning up...$(RESET)"
    rm -f *.o main
    @echo "$(GREEN)Cleanup complete.$(RESET)"
```

In this example:

* **Bold text**: `$(BOLD)`
* **Underlined text**: `$(UNDERLINE)`
* **Red background**: `$(BG_RED)`
* **Green text**: `$(GREEN)`
* **Blue background**: `$(BG_BLUE)`

#### 6. **Tips for Using Colors in Makefiles**

* **Check Terminal Support**: Most modern terminals support ANSI escape codes, but it's always a good idea to check whether your terminal or build environment supports color. You can use `if` conditions in the Makefile to detect if the output is a terminal and apply colors only in that case.

  Example:

  ```makefile
  ifeq ($(shell tput colors), 0)
      NO_COLOR = 1
  else
      NO_COLOR = 0
  endif
  ```
* **Fallbacks**: You can use the `NO_COLOR` variable to avoid color formatting if the terminal doesn’t support it.

  ```makefile
  RED = $(if $(NO_COLOR), , \033[31m)
  RESET = $(if $(NO_COLOR), , \033[0m)
  ```
* **Don’t Overdo It**: Using too many colors or styles can make the output harder to read. It's often best to use colors sparingly to highlight important information, such as errors or successes.

***

#### 7. **Summary of ANSI Escape Codes**

| Code       | Description                         |
| ---------- | ----------------------------------- |
| `\033[31m` | Red Text                            |
| `\033[32m` | Green Text                          |
| `\033[33m` | Yellow Text                         |
| `\033[34m` | Blue Text                           |
| `\033[41m` | Red Background                      |
| `\033[42m` | Green Background                    |
| `\033[1m`  | Bold Text                           |
| `\033[4m`  | Underline Text                      |
| `\033[0m`  | Reset to Default (clear formatting) |

***

#### Conclusion

* **ANSI escape codes** are a powerful way to add color and style to Makefile output.
* You can define color and style variables and apply them to `echo` commands in Makefile rules.
* Combining different escape codes allows you to control both **text color** and **text style** (e.g., bold, underlined).
* Use colors thoughtfully to enhance the user experience without making the output overwhelming.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/all-about-c/makefile/coloring.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
