Compilation
Compilation is normally encapsulated so that all of the necessary steps happen behind the scenes. Type the following C code into a file named count.c
.
#include <stdio.h>
#include <stdlib.h>
#define TARGET 10
int main(){
int i;
for (i = 0; i < TARGET; ++i)
printf("%d\n", i);
return EXIT_SUCCESS;
}
You can compile this code all at once with the following command.
You can verify with ls
that there are now two files: count.c
containing your source code, and count
, the executable program. (If you’re used to Windows where programs have a .exe
extension, you’ll have to get used to the tradition in UNIX that programs do not have an extension at all.) You can run the program and see it work by entering the following.
Compiling
Note that we start from the preprocessed .i
file and that the traditional extension for the assembly language output is .s
. Again, have a look inside the resulting file—you are not expected to understand assembly language yet, but we will be learning a lot about it in the coming weeks.
Assembling
as -o count.o count.s
gcc -c -o count.o count.s
Linking
The assignment
Work through the example above. submit all of the files along the way:
count.c
count.i
count.s
count.o
count
xxd file dumped # dump file into dumped
# now edit dumped
xxd -r dumped file # patch the changes back into the original