Noeud:Compilation, Noeud « Next »:Assembling, Noeud « Previous »:Preprocessing, Noeud « Up »:Pulling it Together
After preprocessing, our next goal is produce assembly language for the
preprocesed file. There are a number of approaches to this; we could
invoke the preprocessor on each .c file, produce a .i file
and then pass this to the compiler; or (a lot less trouble) just pass
the .c files to the compiler, which will detect that the sources
have not been preprocessed, so instead will preprocess them and then run
them through the compiler:
gcc -S -I ../m4 -I ../ main.c freeze.c stackovf.c temp.c
Note that this will produce a number of errors - mine produced the following output:
$ gcc -I. -I.. -I../m4 -S main.c freeze.c stackovf.c temp.c
In file included from main.c:23:
m4.h:60: parse error before `malloc'
m4.h:60: warning: data definition has no type or storage class
m4.h:61: parse error before `realloc'
m4.h:61: warning: data definition has no type or storage class
main.c: In function `main':
main.c:239: `STDIN_FILENO' undeclared (first use in this function)
main.c:239: (Each undeclared identifier is reported only once
main.c:239: for each function it appears in.)
main.c:367: `PACKAGE' undeclared (first use in this function)
main.c:367: `VERSION' undeclared (first use in this function)
In file included from freeze.c:23:
m4.h:60: parse error before `malloc'
m4.h:60: warning: data definition has no type or storage class
m4.h:61: parse error before `realloc'
m4.h:61: warning: data definition has no type or storage class
freeze.c: In function `produce_frozen_state':
freeze.c:205: `PACKAGE' undeclared (first use in this function)
freeze.c:205: (Each undeclared identifier is reported only once
freeze.c:205: for each function it appears in.)
freeze.c:205: `VERSION' undeclared (first use in this function)
In file included from stackovf.c:80:
m4.h:60: parse error before `malloc'
m4.h:60: warning: data definition has no type or storage class
m4.h:61: parse error before `realloc'
m4.h:61: warning: data definition has no type or storage class
$
The reason is that a number of symbols - such as STDIN_FILENO and
VERSION have not been resolved; they were not found in any of the
header files that we specified using -I. This is because we'll
actually reslove these symbols in the last stage (linking) by including
the relevant libraries. Despite these errors, we now have a collection
of .s files:
$ ls -l *.s
-rw-r--r-- 1 rich users 44080 Sep 30 18:25 freeze.s
-rw-r--r-- 1 rich users 20096 Sep 30 18:25 main.s
-rw-r--r-- 1 rich users 810 Sep 30 18:25 stackovf.s
-rw-r--r-- 1 rich users 949 Sep 30 18:25 temp.s
$