CaF2s Webpage

I'm testing NeoCities

The badass tutorial for creating makefiles!

This tutorial may have faults - thats because its a work in progress! Contact me if you think something is wrong.

What is a makefile?

Gnu make is a programming language for optimizing compilation / building. Makefile is just the name of the file.

Or as they write on their webpage: GNU Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files.
Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files. When you write a program, you should write a makefile for it, so that it is possible to use Make to build and install the program.
[1]

How do i start?

First of all you need something to create such as a C/C++ program, Java program och just a pdf from a Latex document.

How to install

Linux users

Gnu make usually comes along when you installed your linux operating system.

If not, then you could install it with for example:

Ubuntu/Debian

sudo apt-get install build-essential

Fedora

sudo yum install gmake

Arch

sudo pacman -S make

Windows users

If you are using Windows then you may install it with for example MSYS2. In msys2 you install it with:

pacman -S make

For the tutorial below you probably also need gcc, install it with:

pacman -S gcc

Tutorial 1: basics

I will first demonstrate how to create a runable binary from a C-file. It is one of the most typical use-cases with the language.

Create a folder somewhere containing:

your folder name

makefile

prog.c

Put the lines below into your C-file (prog.c):

/* Hello World in C */

#include <stdio.h>

int main()
{
	printf("Hello World!\n");
	return 0;
}

Put the lines below into your makefile:

#This is a comment and will be ignored!

all:
	gcc prog.c -o prog
	./prog

Note that the tab before gcc prog.c -o prog is very important!

Run your makefile!

Its time to run the makefile! However you need to navigate to that folder with the command cd. When you feel that you have navigated correctly, try to see that with dir or ls.

When you feel that you are in the correct folder, run:

make

It will then execute:

gcc prog.c -o prog

and then:

./prog

This finally means that it will compile prog.c and create the program prog (gcc prog.c -o prog) which it will run. (./prog)

The output should be:

gcc prog.c -o prog
./prog
Hello World!

What the makefile usually does is just doing ordinary shell commands in a fancy manner.

See also:

Last modified: 13 September 2016 22:54:44. | CaF2 © (All rights reserved)