MUP1+Øvelse+1.2

toc =Opgave beskrivelse=

01.2 - Building programs for host

=Øvelse 1= Vi skal lave et hello world program som skal komplieres manuelt. Koden ser sådan ud: code format="cpp"
 * 1) include

int main{ std::cout << "HELLO WORLD!!!1" << std::endl; return 0; } code Den bliver komplieret således: code format="scheme" stud@ubuntu:~/work/courses/mup/exercise1.1$ g++ -o hello hello.cpp stud@ubuntu:~/work/courses/mup/exercise1.1$ ls hello hello.cpp makefile stud@ubuntu:~/work/courses/mup/exercise1.1$ ./hello HELLO WORLD!!!1 code

=Øvelse 2= Vi skal nu kompilere vores c++ fil via en makefile som ser således ud: code format="bash" SOURCES=hello.cpp OBJFILES=$(SOURCES:.cpp=.o) EXEC=hello CC=g++

hello: hello.o   $(CC) -o $(EXEC) $(OBJFILES)

hello.o: hello.cpp $(CC) -c hello.cpp

clean: @echo "Cleaning project..." @rm $(EXEC) $(OBJFILES)

help: @echo "Commands: help clean" code

Vi kompilere og eksekvere: code format="scheme" stud@ubuntu:~/work/courses/mup/exercise1.1$ make g++ -c hello.cpp g++ -o hello hello.o stud@ubuntu:~/work/courses/mup/exercise1.1$ ls hello hello.cpp hello.o makefile stud@ubuntu:~/work/courses/mup/exercise1.1$ ./hello HELLO WORLD!!!1 stud@ubuntu:~/work/courses/mup/exercise1.1$ make help Commands: help clean stud@ubuntu:~/work/courses/mup/exercise1.1$ make clean Cleaning project... stud@ubuntu:~/work/courses/mup/exercise1.1$ ls hello.cpp makefile code Det virker som det skal.

=Øvelse 3= I denne øvelse skal vi lave et program af flere dele som skal kompileres via en makefile.

Her er vores makefile: code format="bash" SOURCES=main.cpp part1.cpp part2.cpp OBJFILES=$(SOURCES:.cpp=.o) EXEC=parts CC=g++

parts: $(OBJFILES) $(CC) -o $(EXEC) $(OBJFILES)

%.o: %.cpp                       # % tegnet bliver brugt her da vi kan få makefilen til at finde den .cpp fil som passer til .o filen $(CC) -c $<                      # $< er en automatisk variable der indeholder det første prerequsit

clean: @rm $(EXEC) $(OBJFILES)

help: @echo "Commands: clean help" code Vi bruger "kode" til at kompilere alle .o filerne så vi slipper for at skulle skrive dem manuelt. Smart!

Afvikler makefilen og tester at det virker. code format="scheme" stud@ubuntu:~/work/courses/mup/exercise1.1/parts$ make g++ -c main.cpp g++ -c part1.cpp g++ -c part2.cpp g++ -o parts main.o part1.o part2.o stud@ubuntu:~/work/courses/mup/exercise1.1/parts$ ./parts This is part 1! This is part 2! code

Vi tester 'make' funktionalitet til kun at kompilere ændrede filer. code format="scheme" stud@ubuntu:~/work/courses/mup/exercise1.1/parts$ make make: `parts' is up to date. stud@ubuntu:~/work/courses/mup/exercise1.1/parts$ touch part2.cpp stud@ubuntu:~/work/courses/mup/exercise1.1/parts$ make g++ -c part2.cpp g++ -o parts main.o part1.o part2.o code Først så vi at alle filerne var ajure, derefter 'touch' vi en fil og kaldt make igen hvor vi så ser at den kun opdatere den opdaterede fil.

Her tester vi at clean funktionen virker. (hint: det gør den) code format="scheme" stud@ubuntu:~/work/courses/mup/exercise1.1/parts$ ls main.cpp makefile part1.h part2.cpp part2.o main.o part1.cpp part1.o part2.h parts stud@ubuntu:~/work/courses/mup/exercise1.1/parts$ make clean stud@ubuntu:~/work/courses/mup/exercise1.1/parts$ ls main.cpp makefile part1.cpp part1.h part2.cpp part2.h code

=Hvad har vi lært= Vi har lært at kompilere c++ filer og projekter enten manuelt eller via scripts (makefile). Derudover er vi nu hardcore Eclipse superusers of doom! Alt i alt en ganske god dag.