need help understanding makefile

I have a homework assignment but I am struggling to create the makefile. My teacher hasn't provided any walk through examples (and it's an online class).Please help!

here are the directions:
Create a makefile that builds executables test1.x and test2.x. Look at the #include statements in test1.cpp and test2.cpp to deduce what the intermediate targets and dependencies should be.

Design the class ID, placing the definition in file id.h

Implement the class ID, placing the class implementation in file id.cpp. You can test the code for syntax errors with the command "make id.o" or the command "co3330 id".

Thoroughly test class ID, starting out with the supplied test harnesses in file hw3/test?.cpp using your makefile to build the executables. (Note - you could also use the command line compile scripts "co3330" to create object files and then and "g++ -otest1.x id.o test1.o" to create executables, as in Homework 1.)

Turn in id.h, id.cpp, and makefile using the submit script.


_____

so far I have...

id.cpp: id.h
<tab> g++47 -c -Wall - Wextra -I. id.cpp
test1.cpp: id.cpp id.h
<tab> g++47 -c -Wall -Wextra -I. test1.cpp
test2.cpp: id.h
<tab> g++47 -c -Wall -Wextra -I. test2.cpp

I don't even know where to go. We were previously given the makefiles to copy into our directory, and now expected to understand how to do it ourselves. If anyone could help explain this...


_____________

should i be creating the .o files as well..

id.o: id.h id.cpp
<tab> g++47 -c -Wall -Wextra -I. id.cpp
id.cpp: id.h
<tab> g++47 -c -Wall -Wextra -I. test1.cpp
test2.o: id.h id.cpp
<tab> g++47 -c -Wall -Wextra -I. test2.cpp
test.x: id.o test1.o test2.o
<tab> g++47 -otest.x test1.o test2.o id.o





Last edited on
Each target describes how the target (the file) is created and what other dependencies (these are also files) are needed in order to build the target.

1
2
target: dependencies
	commands-to-build-target

As an example, say that to build an executable file named "exefile" you need two object files main.o and foobar.o. The target to build exefile could look something like this.

1
2
exefile: main.o foobar.o
	g++ -o exefile main.o foobar.o

If main.o and/or foobar.o does not exist it will automatically look at the other targets to see if there is a way to create them. To make this work main.o and foobar.o should also be targets. The dependencies for each object file should be the source file + all the files that are included (directly or indirectly), but not the standard header files. Assuming the source files only include one other header file named foobar.h the targets could look something like this:

1
2
3
4
5
main.o: main.cpp foobar.h
	g++ -c main.cpp

foobar.o: foobar.cpp foobar.h
	g++ -c foobar.cpp

That's it! You should not add targets for the code files because these files are not created by the makefile. The reason we had to list the code files as dependencies is so that the makefile can detect if it needs to be recompiled or not.
Last edited on
so i have this

test1.o: id.h id.cpp test1.x
g++47 -c -Wall -Wextra -I. test1.o
test2.o: id.h id.cpp test2.x
g++47 -c -Wall -Wextra -I. test2.o


but it is telling me
test1.o :: too many arguments
okay now i have

id.o: id.h
g++47 -c -Wall -Wextra -I. id.cpp
test1.o: id.h id.cpp test1.x
g++47 -c -Wall -Wextra -I. test1.o
test2.o: id.h id.cpp test2.x
g++47 -c -Wall -Wextra -I. test2.o

i think that may be correct. I now have error in one of my other files so it won't compile though
you don't seem to understand the dependencies

To obtain id.o you need to compile id.cpp, ¿why did you not consider it a dependency?
¿what's doing id.cpp as a dependency of test{1,2}.o?

Also, you do not have any rule that would generate an executable
No help? Did you check the Articles?
http://www.cplusplus.com/articles/jTbCpfjN/

As your compiler is g++47, you'll need to set the environment variable:CXX=g++47
Last edited on
try CMake. http://www.cmake.org/

It's orders of magnitude easier than manually writing a makefile. CMake generates a makefile for you, so you don't have to re-write the bloody thing every time you add/remove a file.

Manually writing makefiles isn't somthing worth doing (the effort involved can be avoided, and, therefore, should be), but if your instructor demands it, then my suggestion isn't really a solution.
Last edited on
No, don't do that. That's just something else you have to learn and learn to fix when it's broken.

Next, someone will be telling you to use the GNU Auto Tools.

These things fall under the category Meta Make Packages because they generate makefiles. But they are more complicated to use than basic makefiles and the makefiles they generate are impenetrable.
It's orders of magnitude easier than manually writing a makefile

not really... cmake is not really well documented. took me much longer than it should have to write a very simple script. make on the other hand has been around for much longer and has much stronger documentation. Especially after reading the GNU Make manual it is quite easy to write simple make scripts.

so you don't have to re-write the bloody thing every time you add/remove a file.

you are writing your make files very wrong if you need to do that.

Manually writing makefiles isn't somthing worth doing (the effort involved can be avoided, and, therefore, should be)

that is terrible logic for multiple reasons. first, it is something worth doing. if you use cmake, you should just blindly trust that your make file is correct? its important to know what you are working with. secondly, programming itself can be avoided all together, so it should be, based on your logic
ok, well, to each his own. I use cmake because I was able to write a script to cover most of my use cases in a day (All I have to do is toggle a few bools and add whatever libraries I need to a list), so for me it's easier than using even an IDE.

Yes you do have to learn it, but it works...
Topic archived. No new replies allowed.