Basics making a ... Makefile

Before going into makefiles let's make sure we understand how and why we would seperate programs into multiple files.


!!!Tutorial Here!!!



Sweet now we can make some makefiles to help automate this process.

You could probably imagen the compile commands getting pretty crazy, somthing like this.

gcc -o myProgram main.c functions.c helpers.c stuff.c myMath.c grid.c -Wall -g -I....


Not only that but you have all your header files, dependincies, clean up after compile, differnt compolations depending on what you need, external files from other locations, ... ect.


Thats where the "make" program comes in, it's popular for C and C++ but all languages have build scripts very simmular to this concept.


Let's get started. First we should create a directory to store all our program files.

[pawprint@babbage ~]$
mkdir test


[pawprint@babbage ~]$
cd test


Now lets add all our program files into that directory.

[pawprint@babbage ~]$
ls


main.c functions.c header.h


Lets add to this our makefile. Makefile will automaticlly look for files with the name of 'makefile' or 'Makefile', it will just be a file with no extention on the end (no .txt or anything)

[pawprint@babbage ~]$
vim makefile


The simplist makefile you would make is a build operation. For our program lets add this text to the file. (Note the tab on line 2 this is important to makefiles it looks for formatting to know the higherarchy of the program, many scripting languages do this)

makeFileTest: main.c functions.c

gcc -o testProgram main.c functions.c


Now all we do to build this script is type 'make' and makefile will call all the approate commands to compile it. (See make is a simpler comand than all that gcc nonsince right?)

[pawprint@babbage ~]$
make


[pawprint@babbage ~]$
./testProgram


Obviously for somthing that small we diden't "need" the makefile but think if you had a ton of files and you had to share it with other people working on the project? What would be easier telling them all the things they need to compile or just have them type make?


This is only scratching the surface of course makefile is a much more powerful program than that, comming soon we will talk about variables, build types, project management and cleaning of files.


Dependency Management

Note from before the makeFileTest: stuff these are the dependincies that makefile will use.


The .o File

Normally when you comple you are doing a "full" compile down to the executable level but when compiling files individually you dont always want to link them to every other piece of the program

To compile to a "object" file we will add a -c flag

gcc functions.c -c

//Creates functions.o