Exercise 1 - The Hello World program
In this
exercise we are to create a c++ program and compile into an executable file
with the compiler g++ (GNU ++ compiler).
We have
created a simple c++ program which will print out a text containing “Hello
World!”.
The above code is then executed in the terminal and we get following print below:
Exercise 2 - using makefiles
Makefile is
a useful tool to implement automation for our software during the building
process. This is because when we have an application containing many files and
we only want to implement/change a small part of the application, we don’t want
to rebuild files that have no influence in the part we are to change or
implement.
With
makefile we can we can instruct the makefile what objects files to change
depending on what source has been changed.
In this
exercise we are to create a makefile for our program hello that contains the
following variables:
· Source files
Object files
The name of the executable
The used compiler.
To start
with, it is recommended to name our make file “Makefile”. We do this by using your text editor Kate. The source
file will be our c++ program hello which will be compiled with into an object
file with g++ compiler. Our executeable file will be named konnichiwa when
compilation is finished.
The first
line contains all our source files that we want to compile. Second line tells
that all variables of sources with cpp extension that is being read shall be
compiled as object files. Third line gives the name of our executeable file.
Fourth line tells what compiler is being used in makefile.
Our first
target which is also defined as default target has only one prerequisite in our
case it is hello.o. Though our syntax is defined as all objects that have been
compiled from SOURCES is our prerequisite.
This means
that whenever one or more prerequisite is fulfilled, it will execute the given
target. What it does is that make validates that all dependencies exist and compare
timestamp. If their timestamp is older than the target, make does nothing
because nothing has changed.
Quote: “The
most important special character in makefiles is the dollar character ($), which
is used to access the contents of variables” – quote from below link http://wiki.maemo.org/Documentation/Maemo_5_Developer_Guide/GNU_Build_System
Exercise 3 - Program based multiple
This is the makefile for exercise 3:
As we see at the picture below, there is only the cpp and header files in the beginning. After the 'make all' command, the object files is made.
With the command 'ls' we see, that both the object files and the executable file 'parts' is made.
With the command 'ls' we see, that both the object files and the executable file 'parts' is made.
With the target help, we see the options we have:
With the command show, we print the text in the two cpp files:
With the command clean we removes the object and executable files:
Here is the pictures of the code in the header and cpp files:
main.cpp
part1.cpp
part1.h
The
difference between exercise 2 and 3 is that we have more than one file that
needs to be compiled into objects and put together into single executeable file
named parts. Here we can see the benefits of makefile.
Exercise 4
How are the
source files compiled to object files , what happens?
There are implicit
rules that apply for the makefile. In our case from the above code snippet, the
object files(.o)is being initiated from its source file(.c). See the following
link: http://www.gnu.org/software/make/manual/make.pdf - under chapter 10 to read further into implicit rules.
Not all
dependencies are handled by this approach, which?
Since source
files usually have its own header file, the implicit rule does not comply. See
above the link to get a list of implicit rules of what file type is handled by this approach
Why is this
a problem?
Because when
changes are being made in a header file, make propagates the change s to the “root”
of the target instead of making the specific changes from header files.
Make keeps track of the last time files (normally object files) were updated and only updates those files which are required (ones containing changes) to keep the sourcefile up-to-date. If you have a large program with many source and/or header files, when you change a file on which others depend, you must recompile all the dependent files. Without a makefile, this is an extremely time-consuming task.
SvarSletWe have read your response to exercise. You have shown that you have understood the meaning of makefiles and how to use them.