Patrick Johnston
Patrick Johnston

Reputation: 101

Can someone show me how to create this very simple makefile?

This is in C, am running UNIX through Putty and want to create a Makefile.

So I have a program that is in only one file "recommender.c" and i need to make a makefile that creates an executable file called recommender using the all function. The part i don't know how to do is that i compile recommender.c using the line gcc recommender.c -std=gnu99 I don't know how to make sure it does the -std=gun99 part too. please help? :)

Upvotes: 1

Views: 1173

Answers (2)

DipSwitch
DipSwitch

Reputation: 5640

CFLAGS=-std=gnu99 -Wall -Wextra -O3

all: recommender

recommender: recommender.o

Note that the file extension must be .c if it has the extension .cpp it will need the CXXFLAGS instead of CFLAGS

Some explanation:

The make file is dependency based as you can see you can set your variables on top of the make file. Then when the make program is invoked it will search for a Makefile in the current directory.

Since the default action is all it will search for the all rule. When it found the all rule it will check which dependencies are needed in this case the recommender rule / file so it starts searching the rules again and it finds out it needs the recommender.o rule / file but it won't find any, now here is where the magic happens.

make is smart enough to know that an .o (object) file is made from an .c or .cpp (source) file so now it will start searching for the source file in the current directory. This he does by replacing the .o with an .c if it finds the file it will compile it with the gcc compiler if it where to find an .cpp file it will compile it with the g++ compiler.

So now we know this we can simply add multiple source files to the project by adding more object file as dependencies.

Now you don't really need to create an object file first since you compile from one source file you can also remove the recommender: recommender.o rule and make will try to create the recommender application by compiling recommender.c or .cpp

Upvotes: 4

Carl Norum
Carl Norum

Reputation: 224864

You don't even need a makefile; CFLAGS="-std=gnu99" make recommender should "just work".

Upvotes: 3

Related Questions