> 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/display-text.md).

# Display text

In Makefile, you can show custom messages after successful compilation or failure using built-in functionality, such as shell commands or special Makefile rules.

Let’s go over how you can display a message after successful compilation or failure.

***

#### Step-by-Step Guide to Show Success or Failure Messages

**1. Display a Success Message After Compilation**

You can simply use `echo` commands to print messages after the compilation step. For example, after successfully compiling the code, you can display "Compilation Successful" or some other custom message.

```makefile
CC = gcc
CFLAGS = -Wall

# Target for compiling the program
main: main.o utils.o
    $(CC) $(CFLAGS) -o $@ $^
    @echo "Compilation Successful!"

# Rule to compile .c files into .o files
%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@
```

* The `@` before `echo` suppresses the command itself from being printed to the terminal. This ensures only the message is shown.
* After the linking rule (`$(CC) $(CFLAGS) -o $@ $^`), the message "Compilation Successful!" is printed if everything works fine.

**2. Display an Error Message on Compilation Failure**

Make will automatically stop execution if a command in a rule fails. However, you can also print custom error messages or handle failures in a specific way using conditional logic.

For example:

```makefile
CC = gcc
CFLAGS = -Wall

# Target for compiling the program
main: main.o utils.o
    @echo "Linking..."
    $(CC) $(CFLAGS) -o $@ $^ || (echo "Linking Failed!" && exit 1)

# Rule to compile .c files into .o files
%.o: %.c
    @echo "Compiling $<..."
    $(CC) $(CFLAGS) -c $< -o $@ || (echo "Compilation of $< failed!" && exit 1)

# Clean up the project
clean:
    rm -f *.o main
```

Explanation:

* `||` is used to handle failure. If the command before `||` fails (e.g., `$(CC)` fails), the commands after `||` are executed.
* `exit 1` ensures the process terminates with an error code, which is standard in Unix-based systems to indicate failure.
* In case of a failure, you see the error message like "Compilation of main.c failed!" or "Linking Failed!".

**3. Display Success and Failure Using Conditional Logic**

You can use `make`'s built-in error handling to display different messages depending on whether the commands succeed or fail:

```makefile
CC = gcc
CFLAGS = -Wall

main: main.o utils.o
    $(CC) $(CFLAGS) -o $@ $^
    @if [ $$? -eq 0 ]; then echo "Compilation and linking successful!"; else echo "Build failed!"; fi

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@
    @if [ $$? -eq 0 ]; then echo "Successfully compiled $@"; else echo "Error compiling $@"; fi

clean:
    rm -f *.o main
```

Explanation:

* `$$?` checks the exit status of the previous command. If it is `0`, the command succeeded, and a success message is shown.
* If the exit status is non-zero (indicating failure), an error message is shown instead.

**4. Using `make`'s Built-In Features for Status**

`make` also has built-in functionality for handling success/failure messages. The `-n` (dry-run) option shows what would happen without actually running the commands, and the `-B` option forces `make` to re-build all targets.

You can use this in combination with custom rules to ensure that the appropriate message is displayed when `make` is run:

```bash
make -n    # Shows what would happen without building
make -B    # Forces rebuild and shows messages accordingly
```

However, these options are more for controlling how `make` executes rather than printing custom success/failure messages.

***

#### Example of Complete Makefile with Success and Failure Handling

Here’s a complete example of a Makefile that shows messages after each step, whether it’s successful or failed:

```makefile
CC = gcc
CFLAGS = -Wall
LDFLAGS = -lm

# Default target
all: main

# Main target depends on object files
main: main.o utils.o
    @echo "Linking program..."
    $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
    @if [ $$? -eq 0 ]; then echo "Compilation and linking successful!"; else echo "Linking failed!"; exit 1; fi

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

# Clean target to remove generated files
clean:
    @echo "Cleaning up..."
    rm -f *.o main
    @echo "Cleanup complete."

# Print the version of the compiler being used
version:
    @echo "Using $(CC) version: $(shell $(CC) --version | head -n 1)"
```

***

#### Output Example

* **Successful Compilation**:

```css
Compiling main.c...
Successfully compiled main.o
Compiling utils.c...
Successfully compiled utils.o
Linking program...
Compilation and linking successful!
```

* **Failed Compilation** (e.g., if there is a syntax error in `main.c`):

```less
Compiling main.c...
Error compiling main.o
make: *** [main.o] Error 1
```

***

#### Summary

* **Success Messages**: Use `echo` after the compilation or linking step to notify the user of successful completion.
* **Failure Messages**: Use `||` or `if [ $$? -eq 0 ]` to check for errors and display custom error messages.
* **Exit with Failure**: Use `exit 1` after an error message to stop the build process.
* **Clean Target**: Include a clean-up target for removing generated files.


---

# 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/display-text.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.
