How to use a makefile

Thread Starter

asilvester635

Joined Jan 26, 2017
73
I've written a makefile and saved it under the name "MakeFile" (provided below) for my program. It's in the same folder as my programs.

all: main

main.o: main.cc Greetings.cc
g++ -c -Werror main.cc

Greetings.o: Greetings.cc Greetings.h
g++ -c -Werror Greetings.cc

main: main.o Greetings.o
g++ -0 main main.o Greetings.o



Now how do I actually use the makefile? I read that you should type in make [options] [target1 target2 ...]. What are options and target? As an experiment, I tried typing "make MakeFile" and I got an error as shown below.

Conrados-MBP:~ conrados$ make MakeFile
make: *** No rule to make target `MakeFile'. Stop.
 

Thread Starter

asilvester635

Joined Jan 26, 2017
73
What compiler are you using? Why are you trying to manually run a make file? There are a number of development environments that will do all that work for you. I have not touched a make file directly in about 20 years.

There are many, many, many articles on how to run make and its options.

https://www.bing.com/search?q=runni...-16&sk=&cvid=C5F78A2F80704188BAF73C0B76751F28
I have a MacBook pro and I'm using sublime text 3 as a text editor for c++. I'm also using my computer's terminal to compile my code, though it's very cumbersome once you have more than one program. Your link that you gave me is for people who have windows. Our professor wants us to get used to using the terminal. He taught us how to create the makefile, but he didn't teach how to actually run it.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
I have a MacBook pro and I'm using sublime text 3 as a text editor for c++. I'm also using my computer's terminal to compile my code, though it's very cumbersome once you have more than one program. Your link that you gave me is for people who have windows. Our professor wants us to get used to using the terminal. He taught us how to create the makefile, but he didn't teach how to actually run it.

Is your professor a 80 years old? Is he also going to force you to use a slide rule too? (And yes I had to learn to use a slide run even though calculators had been around for some time :( ). There is a reason tools like calculators and development environments were created. They improve your productivity giving you more time to be creative rather than bog you down in the tedious details of building a makefile and compiling it manually

Yeah it is probably a good idea to know what is going on in a make file. But I don't know if I once ever had to debug to that level.

At the very least if he wants you to run make, he should demonstrate how to do it. It has been so long since I have touched a make file, I don't think I would remember how to do it. Maybe the reason he has not showed you is he can't remember either. ;)

Do a search. There is plenty of information out there. Including youtube

https://www.youtube.com/results?search_query=makefile+c++
 

MrSoftware

Joined Oct 29, 2013
2,186
Wow also haven't fiddled with makefiles in a long long time. If you're root in your console then you probably need to prepend ./ to the file name. On most systems . is not included in the path for root, for security reasons.

Going entirely from memory, i think it's expecting to find the file named "makefile", all lower case. The name that you enter after make is the name of the target. For example, "make release" to make the target named release, defined in he file called makefile.

Read the man pages for make, you can probably call your file "MakeFile" if you really want to, but you will need to tell make the name of the file with a special parameter. It's easier just to name it makefile, all lowercase. Sometimes capital M is ok, depending on the version of make.

Make can do a lot of stuff.. like a lot a lot of stuff. Read the man pages.
 

thumb2

Joined Oct 4, 2015
122
I always work with makefile named Makefile .
To specify the file, if its name is different than Makefile, I use:
Bash:
make -f <makefile-name>
 
Top