Submission Due: the beginning of your next lab session
In this lab, we review the some of the parts of compilation and program execution and the errors that can be occur. We will try to understand the cause for these errors and fix them.
Try to compile pre-proc1.c:
> gcc pre-proc1.c
Notice that an error and a warning are produced. Now try with the addition of -DCORRECT:
> gcc -DCORRECT pre-proc1.c
This time it should complete successfully. Rerun the previous two commands, this time also passing the -E flag to gcc. Compare the output of those two to help you answer question 1.
Try to compile linker.c:
> gcc linker.c
Once again, it results in an error, but this time a linker error rather than a compilation error. Try it again with the -lm flag added:
> gcc linker.c -lm
Just as before, this time it should succeed. Now answer question 2.
Build a shared library, libmul.so from lib/mul.c:
> gcc -c -fpic lib/mul.c -o lib/mul.o > gcc -shared lib/mul.o -o lib/libmul.so
Now, build linker2.c and link to it libmul.so:
> gcc -c linker2.c -I./include > gcc linker2.o -L./lib -lmul
Compile linker3.c and link libmul.so:
> gcc -c linker3.c > gcc -c foo.c > gcc foo.o linker3.o -L./lib -lmul
Now, use objdump to create a disassembly of linker3.o and a.out:
> objdump -Dr linker3.o > linker3.o.asm > objdump -Dr a.out > a.out.asm
Look in linker3.o.asm and a.out.asm for foo, my_mul, and printf and answer question 3.
Try running a.out:
> ./a.out
It will produce an error because it fails to find libmul.so. Add ./lib to the search path to fix this error:
> LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib ./a.out
Answer question 4.
Write a report answering the following questions and submit a hard copy of the report to your TA by the beginning of the next lab. You do not need to submit source code for this lab nor will a demonstration be required.
How and why does -DCORRECT remove errors and warnings from step 1?
What does -lm do to resolve the error?
What has the linker done in a.out compared to linker3.o?
Why does adding ./lib to LD_LIBRARY_PATH fix the error?