I3ISU_Opgave2

Exercise 1 The Hello World program
hello.cpp file: code format="cpp"
 * 1) include

using namespace std;

int main {   cout << "Hello World!" << endl;   //Outputs Hello World!

return(0); }

code Compile command and executing in console: code $ g++ -o hello hello.cpp $ ./hello Hello World! code This is a simple task, who gives us knowledge about use of text editors and the g++ compiler, and running programs.

Exercise 2 Using makefiles
makefile: code format="make" SOURCES   = hello.cpp OBJECTS   = ${SOURCES:.cpp=.o} EXECUTABLE = hello CC = g++ TARGETS = build clean help build: ${OBJECTS} ${CC} -o ${EXECUTABLE} ${OBJECTS}

clean: @rm ${EXECUTABLE} ${OBJECTS} code Here we use makefiles. Makefiles can make multiple tasks easier to overcome. This is just a simple makefile in order to give us experience with makefiles in general and use of variables in makefiles.

Terminal session: code stud@GoldenImage:~/ISU/exercise2/exercise2_2$ make build
 * 1) g++   -c -o hello.o hello.cpp
 * 2) g++ -o hello hello.o

stud@GoldenImage:~/ISU/exercise2/exercise2_2$ ./hello
 * 1) Hello World!

stud@GoldenImage:~/ISU/exercise2/exercise2_2$ ls
 * 1) hello hello.cpp  hello.o  makefile  makefile~

stud@GoldenImage:~/ISU/exercise2/exercise2_2$ make clean

stud@GoldenImage:~/ISU/exercise2/exercise2_2$ ls
 * 1) hello.cpp makefile  makefile~

code During our terminal session, we tested that everything worked as intented. Both the build and clean worked as intented.

Exercise 3 Program based on multiple files
When building a program made of multiple files, you have to make some changes to your makefile in order to get everything compiled.

First you have to specify all the sources you are going to use code format="make" SOURCES = main.cpp part1.cpp part2.cpp OBJECTS = ${SOURCES:.cpp=.o} code The objects list is automatic generated based on the sources list. Then you have to specify a rule for how to compile every part. It coult look like this code format="make" part1.o: part1.h part1.cpp $(CC) -c part1.cpp code Full makefile can be seen here: http://isu.madsprayer.dk/exc2/makefile.mak

With this makefile we are able to build a program. made of multiple parts. Since the clean function is making use of the Objects list, this is also updated, and will remove the executeable and object files.

Exercise 4 Using Eclipse
After starting eclipse we made a new C++ project and followed the guide and made the Toolchain "Linux GCC". Then we imported the 5 program files from exercise 2.3 by right clicking on the project and choosing import. Then choose general and file system. Then we could navigate to the files files and choose the relevant ones. After importing the files all we had to do was build the project (shortcut Ctrl+B). After building the program we could find it in ~/workspace/ISU_Exc2/Debug/ Eclipse can be a bit difficult to start with at first. But after trying we get a better picture of how to use Eclipse.