Create a Modular Makefile

CBeginner
Practice Now

Introduction

In this challenge, you will learn to create a basic Makefile to compile a simple C program. You'll understand how Makefiles help automate the build process and manage multiple source files.

Create a Basic Makefile

In this challenge, you will create a basic Makefile to compile a simple C program. The program consists of two source files: hello.c and utils.c. The hello.c file contains the main function that prints a message to the console. The utils.c file contains a utility function that is called from the main function.

Tasks

  • Create a Makefile that compiles the program
  • Add rules to compile both source files
  • Add a clean target to remove compiled files

Requirements

  • Use the provided source files in ~/project
  • The Makefile must compile both hello.c and utils.c
  • Create an executable named hello
  • Include a clean target
  • Use GCC as the compiler

Examples

Run the following commands to compile the program:

cd ~/project
make
gcc -c hello.c
gcc -c utils.c
gcc hello.o utils.o -o hello

Run the compiled program:

./hello

Example program output:

Hello, World!
Utility function called!

After running the program, clean up the compiled files.

Hints

  • Remember to use TAB for indentation in the Makefile
  • Use -c flag to compile source files into object files
  • Test the program after compilation

Summary

This challenge introduced you to basic Makefile creation. You learned how to write a simple Makefile to compile multiple source files, create an executable, and clean up build artifacts. These are fundamental skills for managing C programming projects.

✨ Check Solution and Practice