Øvelse+3b+-+Buliding+C+programs+for+target

=Exercise 1 Cross compiling the program Hello World and running it on target=

hello.cpp er kopieret fra tidligere øvelse.

compileren kaldes, og den resulterende fil checkes:

=Exercise 2 Using make files to build host and target programs=

makefile:

code format="make" CC = g++ sources = hello.cpp objects = ${sources:.cpp=.o} target = hello

all: ${objects} @echo "Creating the ${target} executable:"

${CC} ${objects} -o ${target}

@echo "Make complete, run the program using ./${target}"

${objects}: ${sources} @echo "Compiling ${objects} from ${sources}" ${CC} -c ${sources} @echo "\nExcuse me ma'am.. it looks like you have an object infestation\n"

clean:


 * 1) force the rm in case the files don't exist, to reduce nasty errors:

rm -f ${target} rm -f ${objects} @echo "\nTHIS HOUSE. IS. CLEAN!\n" help: @echo "\nLIST OF OPTIONS FOR THIS MAKEFILE:\n" @echo "all : gives you the ${target} sensation!" @echo "object : compiles the *.cpp files into *.o files" @echo "clean : deletes object files and executables" @echo "help: displays this message :)" code

makefile.target: code format="make" CC = arm-angstrom-linux-gnueabi-g++ sources = hello.cpp objects = ${sources:.cpp=.target.o} target = hello.target

all: ${objects} @echo "Creating the ${target} executable:"

${CC} ${objects} -o ${target}

@echo "Make complete, run the program using ./${target}"

${objects}: ${sources} @echo "Compiling ${objects} from ${sources}"

${CC} -c ${sources} -o ${objects}

@echo "\nExcuse me ma'am.. it looks like you have an object infestation\n"

clean:


 * 1) force the rm in case the files don't exist, to reduce nasty errors:

rm -f ${target} rm -f ${objects} @echo "\nTHIS HOUSE. IS. CLEAN!\n" help: @echo "\nLIST OF OPTIONS FOR THIS MAKEFILE:\n" @echo "all : gives you the ${target} sensation!" @echo "object : compiles the *.cpp files into *.o files" @echo "clean : deletes object files and executables" @echo "help: displays this message :)"

code

Screenshot MAKEs:



Screenshot eksekveringer:

=Exercise 3 Cross compiling with more extended make files= Repeat Exercise 2, this time for the program parts from Lab Exercise Building C++ programs for host.

Makefile:

code format="make" CC = g++ sources = main.cpp part1.cpp part2.cpp objects = ${sources:.cpp=.o} target = parts

all: ${objects} @echo "Creating the ${target} executable:"

${CC} ${objects} -o ${target}

@echo "Make complete, run the program using ./${target}"

${objects}: ${sources} @echo "Compiling ${objects} from ${sources}" ${CC} -c ${sources} @echo "\nExcuse me ma'am.. it looks like you have an object infestation\n"

clean:


 * 1) force the rm in case the files don't exist, to reduce nasty errors:

rm -f ${target} rm -f ${objects} @echo "\nTHIS HOUSE. IS. CLEAN!\n" help: @echo "\nLIST OF OPTIONS FOR THIS MAKEFILE:\n" @echo "all : gives you the ${target} sensation!" @echo "object : compiles the *.cpp files into *.o files" @echo "clean : deletes object files and executables" @echo "help: displays this message :)"

code

Makefile.target:

code format="make" CC = arm-angstrom-linux-gnueabi-g++ sources = main.cpp part1.cpp part2.cpp objects = ${sources:.cpp=.o} target = parts.target

all: ${objects} @echo "Creating the ${target} executable:"

${CC} ${objects} -o ${target}

@echo "Make complete, run the program using ./${target}"

${objects}: ${sources} @echo "Compiling ${objects} from ${sources}" ${CC} -c ${sources} @echo "\nExcuse me ma'am.. it looks like you have an object infestation\n"

clean:


 * 1) force the rm in case the files don't exist, to reduce nasty errors:

rm -f ${target} rm -f ${objects} @echo "\nTHIS HOUSE. IS. CLEAN!\n" help: @echo "\nLIST OF OPTIONS FOR THIS MAKEFILE:\n" @echo "all : gives you the ${target} sensation!" @echo "object : compiles the *.cpp files into *.o files" @echo "clean : deletes object files and executables" @echo "help: displays this message :)"

code

FIX:
code format="make" make clean code

before each make* call!!

Calling the make's, and pushing the parts.target file to the devkit:



Execution on host and target: = =

=Exercise 4 Improving cross compilation handling in make files= In Exercises 2 and 3 above, there are two ways: When you have made an executable, say for host, and then re-make it for target, you actually overwrite the host executable. In many cases, this is unacceptable. Furthermore unless you have placed the intermediate created object files in separate directories, then the build process will fail since the linker will try to link object of the wrong type.

Rewrite makefile and makefile.target so that make places the executable(& objects) for host in the sub-directory host/ and the executable(& objects) for target in the subdirectory target/. Also make sure that make clean and make -f makefile.target clean will not remove the opposite executable(& objects) - in other words, make clean must not remove the target executable(& objects), and make -f makefile.target clean must not remove the host executable(& objects). Make no mistake this exercise will take time and it would be very prudent to determine ahead of time, how the dierent challenges should be dealt with.

Makefile:
code format="make" CC = g++ sources = main.cpp part1.cpp part2.cpp target_dir = bin/host

objects = $(addprefix ${target_dir}/, ${sources:.cpp=.o})
 * 1) include target path in objects variable:

target = parts

${target_dir}/target: ${objects}

@echo @echo @echo "Creating the ${target} executable in ${target_dir}:"

${CC} ${objects} -o ${target_dir}/${target} @echo @echo @echo "Make complete, run the program in the ${target_dir} folder using: ./${target}" @echo

${target_dir}/%.o: %.cpp

@echo @echo @echo "compiling $<"

mkdir -p ${target_dir}

${CC} -c $< -o $@

clean:


 * 1) force the rm in case the files don't exist, to reduce nasty errors:

rm -rf ${target_dir} @echo "\nTHIS HOUSE. IS. CLEAN!\n" help: @echo "\nLIST OF OPTIONS FOR THIS MAKEFILE:\n" @echo "all : gives you the ${target} sensation!" @echo "all files are output to ${target_dir}" @echo "clean : deletes object files and executables" @echo "help: displays this message :)" code

Makefile.target:
code format="make" CC = arm-angstrom-linux-gnueabi-g++ sources = main.cpp part1.cpp part2.cpp target_dir = bin/target

objects = $(addprefix ${target_dir}/, ${sources:.cpp=.o})
 * 1) include target path in objects variable:

target = parts.target

${target_dir}/target: ${objects}

(Resten af filen er som ovenstående makefile) code

Bonusinfo:
forskellen mellem de to filer kan checkes med kommandoen: code format="bash" diff makefile makefile.target code

denne kommando outputter følgende: code 1c1 < CC = g++ --- > CC = arm-angstrom-linux-gnueabi-g++ 3c3 < target_dir = bin/host --- > target_dir = bin/target 8c8 < target = parts --- > target = parts.target code

Cleaning:


=Exercise 5 Using Eclipse instead of make files= Open your solution to Lab Exercise Building C++ programs for host in Eclipse and add a con guration \Target" to your project. Set it up for target build and use it to build the program for target. Transfer the executable to target and execute it. NOTE: Be sure to complete this exercise. At a later lecture we will be cross debugging in eclipse. Having at least a fundamental understanding of eclipse will therefore be of huge help.

1. Create and build a C++ project for host
Start Eclipse by the command. Then create a new C++ project (File > New... > C++ Project), add files and compile the project.

2. Create a new build configuration for target
By default, your project already contains two // build configurations //, and. For each of these, you can set up how to build them etc. We will create a third Build Configuration, Which we will set up to build the C++ project for the DevKit8000.

1. Right-click your project, select "Build Configurations > Manage...". Then select "New..." 2. Name the configuration "Target" and add an optional description. 3. Select "OK" 4. Right-click your project again, select "Properties > C/C++ Build > Settings" 5. Select "Target" from the "Configuration" drop-down menu 6. In "Tool Settings", change the following: 7. Right-click your project, select "Build Configurations > Set Active > Target" 8. Build your project
 * For "GCC C++ Compiler", change to
 * For "GCC C Compiler", change to
 * For "GCC C++ Linker", change to
 * For "GCC Assembler", change to

Building:


Your target executable is now in the subdirectory of your project directory. Transer this file to target and execute it.

Executing:
[IMAGE SOON...]

=Exercise 6 Merging make file and make file.target (OPTIONAL)= The solution found in exercise 4 has but one flaw and that is that we now have to maintain two different make files. A huge improvement would therefore be to merge the two make files. If you choose to do so, then the following should yield an executable viable for the target:

//make ARCH=target//

The following should produce an executable for host:

//make ARCH=host//

If nothing is specified it should produce an executable for host. This feature is obviously done by using conditionals in make, how this is done is something google can help with...

While you are at it, what about handling debug build as well? :-) A debug build for target could be something like:

//make ARCH=target DEBUG=y//

code sources = main.cpp part1.cpp part2.cpp

ifeq (${ARCH},target) CC = arm-angstrom-linux-gnueabi-g++ executable_dir = bin/target executable += parts.target else CC = g++ executable_dir = bin/host executable = parts endif
 * 1) if ARCH == target

ifeq ($(DEBUG), y) CFLAGS = -g else CFLAGS = endif
 * 1) debug?

objects = $(addprefix ${executable_dir}/, ${sources:.cpp=.o})
 * 1) include target path in objects variable, just to show you how.. :)

${executable_dir}/executable: ${objects}

@echo @echo @echo "Creating the ${executable} executable in ${executable_dir}:"

${CC} ${CFLAGS} ${objects} -o ${executable_dir}/${executable} @echo @echo @echo "Make complete, run the program in the ${executable_dir} folder using: ./${executable}" @echo

${executable_dir}/%.o: %.cpp

@echo @echo @echo "compiling $<"

mkdir -p ${executable_dir}

${CC} ${CFLAGS} -c $< -o $@

clean:


 * 1) force the rm in case the files don't exist, to reduce nasty errors:

rm -rf ${executable_dir} @echo "\nTHIS HOUSE. IS. CLEAN!\n" help: @echo @echo "\nLIST OF OPTIONS FOR THIS MAKEFILE:\n" @echo @echo "specify target with ARCH=target or ARCH=host" @echo "as in: make ARCH=target" @echo @echo "all files are output to the bin/ dir" @echo "clean : deletes object files and executables" @echo "help: displays this message :)" code