Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Use a Makefile instead of just writing C to do the same?

Tags:

c

makefile

This question is not opinion-based - I am looking for objective drawbacks to the easy approach that I know may not be ideal, but seems quite alright prima facie.

EDIT: I do not mean to imply that in some way I am writing my own Make in C - no, I'm only wondering if, in simple and small programs, this sort of approach has any drawbacks over make.

Now, the question.

C is a general purpose, systems-programming oriented language, so it is safe to assume that one can hack-together anything with it.

Of course, we have different languages and tools allowing us to use the best tool for the job, and Makefiles are specifically for compiling efficiently and with least hassle - this I get.But, one can basically do with C what they do with a makefile - and apparently with no more difficulty !?

Apart from 'not using industry standards', what objective drawbacks does such an approach bring ?

A bad 2-minute sample, by no means perfect or finished, and without the file-selective compilation, but just to kind of illustrate my point -

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("\n Mode : "); int mode; scanf("%d",&mode);

    if(mode==1){
        system("cc -Wall -o3 file1.c file2.c -o bin_file_name");
    }
    else if(mode==2){
        system("cc -Wall -Wextra -pedantic -Wconversion -o0 file1.c file2.c -o bin_file_name");
    }
    return 0;
}
like image 766
user13863346 Avatar asked Dec 06 '25 06:12

user13863346


2 Answers

You could write a C program which efficiently builds a given project. This is what you shown code seems to imply.
For more complex projects that would of course be more complex.
Soon you would develop your program to do some analysis (e.g. dependencies) automatically, instead of hardcoding it each time.
Then you would discover, that from project to project, those analysis need to take different, project-specific, concepts and structures into account.
That would introduce the concept of project-specific configuration into your program.

At that point you would have reinvented the make tool, with its project-specific configuration done in files like makefiles.

like image 158
Yunnosch Avatar answered Dec 07 '25 19:12

Yunnosch


make does many things more than your toy example does. Here are just a few of the most common:

  • It knows how to compile programs in lots of languages, and you can easily extend it with additional rules.
  • It checks dependency relationships, so that it only recompiles the files that have changed, and then recompiles other programs that depend on those outputs.
  • You can organize your project into groups of code, and specify which groups should be rebuilt.
  • You can provide optional parameters, which will be substituted into the commands.

Getting all these details right is complex. Why would you want to spend time writing your own program to do these, when what you really want to work on is your application? And you would have to write a new one of these programs for every project.

Of course, each of these programs would be very similar. You would probably write a library of functions to handle the common tasks. Guess what -- your library would be doing what make already automates.

You can also do what awk and sed do by writing C programs. But the whole point of creating tools is that they automate tasks that lots of different people have to do the same way.

like image 42
Barmar Avatar answered Dec 07 '25 21:12

Barmar