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.
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...
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: dependenciescommands-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.
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:
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.
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.
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.