How to create a Makefile?


I've been looking around on the internet for the use of makefiles and the make command. I found some great tutorials regarding syntax and how to write a good makefile, and this is great. But I have one problem though:

None of these tutorials explained how to physicalle CREATE a Makefile. I mean; do I use a text editor and simply call the file "Makefile"? I tried that but he would always save it as "Makefile.txt", this won't work. Any help is appreciated.
Yes you can use a text editor. The makefile is usually named simply Makefile without a suffix. To execute the makefile you need to have a make tool, like GNU Make installed. Simply run make to run the default all target.
I tried this without suffix but it keeps saving it as Makefile.txt
Which editor are you using? Some have a default extension they'll add on if you forget.

And are you working on Windows?

Guessing you might well be...

Newer versions of Windows (and Notepad) can be a bit too "helpful". But you can...

1. After saving and closing the file, use Explorer to find and rename it. Or you could rename it from the console. Once it's got the name makefile, it'll stay that way.

(Aside: have you configured Explorer to always show file extensions?)

2. Quote the file name when saving. i.e. save "makefile"

Last edited on
Great thanks! I do use windows. I know it's not optimal but I'm not much of a programmer at all. I just happen to follow a course out of interest, but I don't want to start using Unix just for a course.

I managed to cancel the suffix by saving it as Makefile. windows is indeed too helpfull and I couldn't find anywhere how to cancel it. I read your post right after this ... xD
u can run a makefile with gcc make
Ok so here is another question:

When I launch cmd, he targets a default directory. Before I can use make I have to change this directory with "cd PATH" command. Then I juste use make and it works fine.

But is there a way to do this in one command? Something that would say "Run make in _directory" ?

Thanks again!
man make wrote:
-C dir, --directory=dir
Change to directory dir before reading the makefiles or doing anything else.
Topic archived. No new replies allowed.