How to write makefile in linux for c


Advertisements

Share



Learn how to create a makefile for a software project, build the project and create executables for the project using makefile.

Creating Linux makefile, build project images

Frequently asked topics, concepts.
  • What is a makefile?
  •     Make is Unix utility that is designed to start execution of a makefile. A makefile is a special file, containing shell commands, that you create and name makefile. While in the directory containing this makefile, you will type make and the commands in the makefile will be executed. If you create more than one makefile, be certain you are in the correct directory before typing make.
        Make keeps track of the last time files (normally object files) were updated and only updates those files which are required (ones containing changes) to keep the sourcefile up-to-date. If you have a large program with many source and/or header files, when you change a file on which others depend, you must recompile all the dependent files. Without a makefile, this is an extremely time-consuming task.
        As a makefile is a list of shell commands, it must be written for the shell which will process the makefile. A makefile that works well in one shell may not execute properly in another shell.
        The makefile contains a list of rules. These rules tell the system what commands you want to be executed. Most times, these rules are commands to compile(or recompile) a series of files. The rules, which must begin in column 1, are in two parts. The first line is called a dependency line and the subsequent line(s) are called actions or commands. The action line(s) must be indented with a tab.
    basic.c program illustrating makefile and applications.
    
    #include <stdio.h>
    #include "basic.h" 
    int total,tot = 0;
     int main ()
     {
         printf("This is a simple C Program\n");
         total = sum(1,2);
         printf("Sum of two numbers is %d\n", total );
       return 0; 
     }
    
    basic.h program to be used in makefile.
    
    int sum (int, int);
    
    basic1.c program to be used in makefile.
    
    #include "basic.h" 
    int sum (int a, int b)
    {
      return a + b;
    }
    
    makefile for executing basic.c program.
    
    #########################################################################################################
    # This is an example Makefile for illustration of handling multiple .c .h programs from a single directory. This
    # program uses the basic.c,basic.h,basic1.c programs.
    # Typing 'make' or 'make basic' will create the executable file.
    # Then execute the executable using ./basic
    # define some Makefile variables for the compiler and compiler flags
    # to use Makefile variables later in the Makefile: $()
    #
    # the compiler: gcc for C program, define as g++ for C++
    #########################################################################################################
     CC = gcc
    
    # compiler flags:
    #  -g    adds debugging information to the executable file
    #  -Wall turns on most, but not all, compiler warnings
    CFLAGS  = -g -Wall
    
    # typing 'make' will invoke the first target entry in the file 
    # (in this case the default target entry)
    # you can name this target entry anything, but "default" or "all"
    # are the most commonly used names by convention
    #
    # the build target executable:
    TARGET = basic
    
    all: $(TARGET)
    
    # To create the executable file basic we need the object files
    # basic.o basic1.o :
    #
    $(TARGET): basic.o basic1.o 
      $(CC) $(CFLAGS) -o $(TARGET) basic.o basic1.o 
    
    basic.o: basic.c basic.h 
      $(CC) $(CFLAGS) -c basic.c  
    
    basic1.o: basic1.c basic.h
      $(CC) $(CFLAGS) -c basic1.c
    
    clean:
      $(RM) basic *.o *~
    

    Exercises, Solutions

    Medium
    Medium
    High
    Low
    Medium
    Low
    Medium
    Low
    Medium

    Want to contribute a new article? Write and upload your article information .
    Share

     Articles

     C Programming

     Linux Software

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs

    Glass Elite Apple iPhone 11 Pro (Case Friendly)

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software