20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Divid<strong>in</strong>g Your Program <strong>in</strong>to Multiple Files<br />

335<br />

the result<strong>in</strong>g object code <strong>in</strong>to .obj files <strong>in</strong>stead of .o files.) Typically, these <strong>in</strong>termediate<br />

object files are automatically deleted after the compilation process ends. Some C compilers<br />

(and, historically, the standard Unix C compiler) keep these object files around and<br />

do not delete them when you compile more than one file at a time.This fact can be<br />

used to your advantage for recompil<strong>in</strong>g a program after mak<strong>in</strong>g a change to only one or<br />

several of your modules. So <strong>in</strong> the previous example, because mod1.c and ma<strong>in</strong>.c had no<br />

compiler errors, the correspond<strong>in</strong>g .o files—mod1.o and ma<strong>in</strong>.o—would still be around<br />

after the gcc command completed. Replac<strong>in</strong>g the c from the filename mod.c with an o<br />

tells the C compiler to use the object file that was produced the last time mod.c was<br />

compiled. So, the follow<strong>in</strong>g command l<strong>in</strong>e could be used with a compiler (<strong>in</strong> this case,<br />

cc) that does not delete the object code files:<br />

$ cc mod1.o mod2.c ma<strong>in</strong>.o –o dbtest<br />

So, not only do you not have to reedit mod1.c and ma<strong>in</strong>.c if no errors are discovered by<br />

the compiler, but you also don’t have to recompile them.<br />

If your compiler automatically deletes the <strong>in</strong>termediate .o files, you can still take<br />

advantage of perform<strong>in</strong>g <strong>in</strong>cremental compilations by compil<strong>in</strong>g each module separately<br />

and us<strong>in</strong>g the –c command-l<strong>in</strong>e option.This option tells the compiler not to l<strong>in</strong>k your<br />

file (that is, not to try to produce an executable) and to reta<strong>in</strong> the <strong>in</strong>termediate object<br />

file that it creates. So, typ<strong>in</strong>g<br />

$ gcc –c mod2.c<br />

compiles the file mod2.c, plac<strong>in</strong>g the result<strong>in</strong>g executable <strong>in</strong> the file mod2.o.<br />

So, <strong>in</strong> general, you can use the follow<strong>in</strong>g sequence to compile your three-module<br />

program dbtest us<strong>in</strong>g the <strong>in</strong>cremental compilation technique:<br />

$ gcc –c mod1.c Compile mod1.c => mod1.o<br />

$ gcc –c mod2.c Compile mod2.c => mod2.o<br />

$ gcc –c ma<strong>in</strong>.c Compile ma<strong>in</strong>.c => ma<strong>in</strong>.o<br />

$ gcc mod1.o mod2.o mod3.o –o dbtest Create executable<br />

The three modules are compiled separately.The previous output shows no errors were<br />

detected by the compiler. If any were, the file could be edited and <strong>in</strong>crementally recompiled.The<br />

last l<strong>in</strong>e that reads<br />

$ gcc mod1.o mod2.o mod3.o<br />

lists only object files and no source files. In this case, the object files are just l<strong>in</strong>ked<br />

together to produce the executable output file dbtest.<br />

If you extend the preced<strong>in</strong>g examples to programs that consist of many modules, you<br />

can see how this mechanism of separate compilations can enable you to develop large<br />

programs more efficiently. For example, the commands<br />

$ gcc –c legal.c Compile legal.c, plac<strong>in</strong>g output <strong>in</strong> legal.o<br />

$ gcc legal.o makemove.o exec.o enumerator.o evaluator.o display.o –o superchess<br />

could be used to compile a program consist<strong>in</strong>g of six modules, <strong>in</strong> which only the module<br />

legal.c needs to be recompiled.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!