Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Build Your Own 'cat' Command in C for Linux
Next article icon

How to use make utility to build C projects?`

Last Updated : 19 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

When we build projects in C/C++, we have dependencies among files. For example, there might be a file a.c that calls a function from b.c. So we must compile b.c before a.c. There might be many dependencies in a project and manually following these dependencies and compiling files one by one becomes difficult. In this article, we are going to see how make utility can help us in making this simple. First we need to create 4 files in which 2 of them are .c files and 1 header ( .h ) file and 1 make ( .mk ) file. 

Let us name the files as client.c and server.c and server.h and makefile.mk
Makefile is a set of commands (similar to terminal commands) with variable names and targets to create object file and to remove them. In a single make file we can create multiple targets to compile and to remove object, binary files. You can compile your project (program) any number of times by using Makefile. The main idea of this file is to specify the dependencies.

make utility : This is a command line utility and used to process the instructions written in the Makefile.
Let us take a simple example. The client.c file contains the main function, the server.c file contains the user defined function, 
The third file which is server.h header file which calls the user defined functions in the server.c file and the fourth file which is makefile.mk which contains set of all the commands with their variable names. 

Here we write the client.c file

C




// This is client.c file
#include "stdio.h"
 
// This is header file that we have created
// in the beginning.
#include "server.h"
int main()
{
    printf("hey there, welcome to ");
    greetings();
    return 0;
}
 
 

This is the client.c file which contains two header files one is #include”stdio.h” and the other is #include”server.h” file and remember this is the same file that we have created in the beginning and this contains main function which contains the printf statement which prints as “hey there, welcome to” (without quotes) and the main functions also calls the other user defined function which is greetings() . 
Now we write the server.c file 
 

C




// This is server.c file
#include "server.h"
#include "stdio.h"
void greetings()
{
    printf("geeksforgeeks !");
}
 
 

In this server.c which includes the two header files one is #include”stdio.h” and the other is #include”server.h” file and remember this is the same file that we have created in the beginning and this contains user defined greetings function which contains the printf statement which prints as “geeksforgeeks !” (without quotes). 

Now we write the server.h file 

C




// This is server.h file
void greetings();
 
 

This server.h file is very simple this calls the functions which are written in that file and when we include this header file to the other c programs then we can use the functions defined in this header file. Here this server.h file includes the functions wherever it is included. 

Now we write the makefile.mk file

C




// This is makefile.c file
a : client.o server.o gcc
        client.o server.o client.o : client.o server.h gcc
    - c client.c server.o : server.o server.h gcc
    - c server.c
 
 

Now read this carefully here i will show you how to write makefile, 

This is in windows so the object file is “a” if you are using the Linux, you replace “a” by “a.out”(without quotes) 
See the first line of the code in that “a” represents the object file which contains all the code that we wrote till now, after “a” there are two more object file they are client.o and server.o these are the object files which are required to make the object file “a”, in the next line there is gcc command remember this there should be 1 Tab space before writing the gcc command( if you forgot to put the tab this program will not run), the gcc command compiles the files given to it and stores in the name of its object files. 

This is easily understood like here,
target target_name : prerequisites 
command with tab space

Other way to remember is 
food: food items 
making food 

Now lets move to the third line here the client.o is needed(because this is used in first line of code)so the pre-requisites for that file are client.o and server.h file the gcc command will compile the client.c to get the client.o file. 
The last thing we need is server.o file to get that file we need server.o object file and the server.h header file 
the gcc compiler will compile the server.c file to get the server.o file 
now all things we need is ready, the makefile code is complete now. 

Now will see how to run the make file. 

// This is used to run the makefile 
make -f makefile.mk

This is the syntax to run the makefile after typing this press enter the code will compile and that will create an executable file named “a” (in windows), “a.out”(in linux). 

Now to execute the file it’s here 

// Remember this should be done only after the makefile command is executed 
// This in windows 
a 
//this in linux 
./a.out

Examples:  

Output : hey there, welcome to geeksforgeeks

How does make utility work internally? It creates a dependency graph of tasks and uses Topological Sorting algorithm to find a valid sequence that follows all of the given dependencies.



Next Article
Build Your Own 'cat' Command in C for Linux
author
gurukiranx
Improve
Article Tags :
  • C Language
  • C Programs
  • C++
Practice Tags :
  • CPP

Similar Reads

  • How to create a Heart using C Graphics
    Prerequisite: graphics.h, How to include graphics.h? The task is to write a C program to draw a Heart using graphics in C. Approach: To run the program we have the include the below header file: #include <graphic.h> We will create a Heart with the help below functions: rectangle(x1,y1,x2,y2):
    2 min read
  • Build Your Own 'cat' Command in C for Linux
    You may have heard about cat command which is a Linux command. It stands for concatenate and plays an important role in Unix-like operating systems by helping to concatenate and display file contents. Despite the simple name, cat does a lot of work and goes beyond just putting those files together.
    7 min read
  • How to Write a Command Line Program in C?
    In C, we can provide arguments to a program while running it from the command line interface. These arguments are called command-line arguments. In this article, we will learn how to write a command line program in C. How to Write a Command Line Program in C? Command line arguments are passed to the
    2 min read
  • Create Directory or Folder with C/C++ Program
    Problem: Write a C/C++ program to create a folder in a specific directory path. This task can be accomplished by using the mkdir() function. Directories are created with this function. (There is also a shell command mkdir which does the same thing). The mkdir() function creates a new, empty director
    2 min read
  • C Program to create a House using Graphics
    Prerequisite: graphics.h, How to include graphics.h in CodeBlocks? The task is to write C program to create a house using graphics. To run the program we have the include the below header file: #include <graphic.h> Setting Up the Environment: Download the WinBGlm zip file from this link. Extra
    3 min read
  • C Exercises - Practice Questions with Solutions for C Programming
    The best way to learn C programming language is by hands-on practice. This C Exercise page contains the top 30 C exercise questions with solutions that are designed for both beginners and advanced programmers. It covers all major concepts like arrays, pointers, for-loop, and many more. So, Keep it U
    12 min read
  • C Program to Show Types of Errors
    Here we will see different types of errors using a C program. In any programming language errors are common. If we miss any syntax like parenthesis or semicolon then we get syntax errors. Apart from this we also get run time errors during the execution of code. There are 5 types of error in C: Synta
    4 min read
  • dos.h header in C with examples
    dos.h is a header file of C Language. This library has functions that are used for handling interrupts, producing sound, date and time functions, etc. It is Borland specific and works in compilers like Turbo C Compiler. Below are the functions supported by this library: delay(): The delay() function
    4 min read
  • CLI programs in C for playing media and shut down the system
    Command Line Interface: CLI is a text-based user interface (UI) used to view and manage computer files.Command-line interfaces are also called command-line user interfaces, the console uses interfaces and characters uses interfaces.In programming, the user gives input during the execution of a progr
    6 min read
  • C program to print digital clock with current time
    The time.h header defines four variable types, two macro and various functions for manipulating date and time. A brief description of the variable types defined in the header time.h is as : size_t : This is the unsigned integral type and is the result of the sizeof keyword.clock_t : This is a type s
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences