How do u use a Makefile to make a small little program so that i can get some output? What is a makefile?

Make is a program that applies dependencies rules and perform actions according to the specified rules.

The makefile is usually the name of the files holding the dependencies rules. It`s commonly used by program to automate the compilation and linking phases of building an executable from source code. Typical an executable depends on a set of object files (.o) which themselves dependes on some C files and H files.

The make file automates the tedious task of compling/linking an execute, but you have to provide the source ;-=) … /make.html

I think i got a small little makefile going. I tried to make a little file with like a:
printf (“hello”);

so that i can see that I am doing it right, but it won’t print. All I have to do is type in the command line:

make -f name

right? and it should print that line out if that file containing that line is in the makefile right?

Here’s what i have…

edit : main.o
cc -o edit main.o

main.o : main.c
cc -c main.c

That’s in a file called edit.
Then i have another file called main.c:

printf (“hello”);
return 0;

Can someone please help me here? How come I can’t get a simple message to print out using a makefile?

Wrong, (did you take the time to read my previous post).

A makefile doesn’t generate a program, it only execute them. In your case you are providing a program in C code, which needs to be compile and linked first! The result is an executable. A makefile is NOT an executable. Typicaly the makefile will call the compiler to build the executable. In your case that is what you are trying to do.

Here’s what i have…

You named your makefile (the filed used by make) edit. That’s ok. But cc will generate an executable called edit, which mean it will overwrite the makefile. Not good. Rules of thumb, always name your makefile “makefile” :wink: Then you can just run make and don’t have to specify a filename as make looks for file call Makefile or makefile.

Once make as done it’s job of starting the compiler to compile your source the result is an executable called edit. Make will NOT run edit, it will just BUILD it.

Once built, you can then manually start the resulting executable called edit.

I assume you are using pseude code here, just in case:

#include <stdio.h>

int main(void)
return 0;

Is better.

Makefile is not a compiler and cannot on it’s own generate an executable. All make can do is call other program (i’m oversimplifying).

I did read your post and a lot of the information on the link but I am a beginner and was getting a bit confused with some of the vocabulary like executables and their differences from other files like sources. But I think I got it now. Let’s see…
So i changed the name of edit to Makefile. Then using make, the makefile (Makefile) called the compiler and built my executable (edit). (main.c is my source?) And if i try make again it says edit is up to date. Which means it is compiled correct? Now I can run my program by specifying the path and file… and i get my hello. good.

How come i was reading something about a.out being used to run a program?

thanks for the help Mario. big time.

Yes exactly.

That is a correct assumption although not 100% right. All make does is check if the .o is younger then the .c and if edit is youger then the .o. When makefile says “edit is up to date” it is telling you all the rules are respected. Doesn`t mean the executable is good, just means it exists.

What happens is if you edit the .c, it becomes younger then the .o, so make call the compiler to build the .o. Onces that done the makefile notices edit is older then the .o and thus calls the linker to generate edit. If you were to delete edit and call make you will notice the makefile only calls the linker and doesnt compile the .c into a .o.

Building an executable is done in two steps. First the compiler parses the .c and generates a .o (called an object file). Then these object files must be linked together to create an executable. That`s called linking. cc can compile and link in one step, but in your makefile the -c option tells cc to only perform compilation and NOT run the linking phase which is handle by the other rules in your makefile.

Yes, the executable should be located in the same directory as the source. You should be able to start it by prefixing “./” hence in your case you would type ./edit. No need to type the full path.

a.out is the default name given to an executable when the linker is not told what name to give. In your makefile you are specifying a name “-o edit”.

You could also generate the executable without using the makefile, by manually calling the compiler “cc main.c -o edit”. One draw back of this solution is that it takes more keystroke then just typing make :wink: The principal drawback is that cc will everytime compile and link which can be a big deal if your executable is build from multiple source file. Compiler can compile more then source file and link them to create a big executable.

Make shows its muscle when there are lots of source file. Some project can include hundred if not thousands of source files. These can take long time to build (some can take hours) so you do want to recompile all the source files when all you did is made a change to one of them :wink:

My pleasure!