4.2.3 Multi-Step C Compilation

A multi-step compilation method can be employed to build projects consisting of one or more C source files. Make utilities can use this feature, taking note of which source files have changed since the last build to speed up compilation. Incremental builds are also be performed by integrated development environments.

Make utilities typically call the compiler multiple times: once for each source file to generate an intermediate file and once to perform the second stage compilation, which links the intermediate files to form the final output. If only one source file has changed since the last build, the intermediate file corresponding to the unchanged source file need not be regenerated.

For example, the files ex1.c and add.c are to be compiled using a make utility. The command lines that the make utility could use to compile these files might be something like:

xc8-cc -mcpu=16F877A -c ex1.c
xc8-cc -mcpu=16F877A -c add.c
xc8-cc -mcpu=16F877A  ex1.p1 add.p1

The -c option, used with the first two commands, will compile the specified file into the intermediate file format, but not perform the link step. The resultant intermediate files are linked in the final step to create the final output ex1.elf. All the files that constitute the project must be present when performing the second stage of compilation.

Note: Always use p-code files (.p1 extension) as the intermediate file format for C source.

The above example uses the command-line driver, xc8-cc, to perform the final link step. You can explicitly call the linker application, hlink, but this is not recommended as the commands are complex and when driving the linker application directly, you must specify linker options, not driver options, as shown above.

You may also wish to generate intermediate files to construct your own library archive files.