User3
User3

Reputation: 2535

Understanding Make in C

I am a seasoned developer in Java and I had learned C in my College days, however I am brushing up my C skill and am following a tutorial from Here

I am trying to follow a tutorial on makefile here is what the author says:

Does the file ex1 exist already?
No. Ok, is there another file that starts with ex1?
Yes, it's called ex1.c. Do I know how to build .c files?
Yes, I run this command cc ex1.c   -o ex1 to build them.
I shall make you one ex1 by using cc to build it from ex1.c.

But I am unable to grasp, what is a makefile and why is it used? What are the parameters to the same? Also what are CFLAGS? What is CC? I am new to Ubuntu although.

Upvotes: 0

Views: 352

Answers (1)

Floris
Floris

Reputation: 46365

Good explanation would be very long.

Short explanation: makefile is a set of instructions on how to compile / build an executable. It includes all relationships. For example, "executable A needs object files B and C. B is compiled from files X.c X.h Y.c and Y.h; C depends on K.c". Now if someone modifies K.c, you know you need to recompile C but you don't need to recompile B (just link B and C into A at the end).

As projects get more complicated this becomes more important.

As for flags - there are all kinds of ways to control your compiler. Sometimes you will want to change these - say, you want to include more debug, or increase level of optimization; or change the target architecture. All these things are controlled with flags. By setting a variable to contain these flags, you can replace the flags in many commands at the same time. You can even change what compiler you want to use - you might have different ones as your source code might contain more than one language (mixtures of C and FORTRAN are still encountered in many numerical analysis libraries, for example.)

cc is a C compiler. So is gcc (the Gnu C Compiler). Other compilers include g++ (for C++), g77 (for FORTRAN77), etc...

All of this means that using a makefile is great for maintaining control and flexibility when compiling large and complex projects.

But really - you need to read up about this properly and not rely on something that was written late at night...

Upvotes: 3

Related Questions