15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files....

7
15. WRITING LARGE PROGRAMS

Transcript of 15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files....

Page 1: 15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.

15. WRITING LARGE PROGRAMS

Page 2: 15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.

Source Files• A program may be divided into any number of source files.

• Source files have the extension .c by convention.

• Source files contain definitions of functions and variables.

• One source file must contain a definition of main (the program’s entry point).

Page 3: 15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.

Source Files

• Dividing a program into multiple files has several advantages:Related functions can be grouped together into a single file, making the structure of the program clearer.Each source file can be compiled separately.A single source file can be shared by several programs.

Page 4: 15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.

Header FilesHeader files contain information to be shared among several source files.

• Header files are a convenient place to put macro definitions. A header file named boolean.h might contain the following lines:

#define BOOL int#define TRUE 1#define FALSE 0

Other files can access these macro definitions by using the directive

#include "boolean.h"

Page 5: 15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.

Header Files• Putting macro definitions in a header file has several advantages:

We don’t have to copy the definitions into each source file.

Changing the definition of a macro requires only that we edit a single header file.

We don’t have to worry about the possibility that different source files might contain different versions of the macro.

Page 6: 15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.

Protecting Header Files• If a source file includes the same header file twice, compilation errors may result, depending on the contents of the header file.

• This problem is common when header files include other header files:header1.h includes header3.hheader2.h includes header3.hprog.c includes header1.h and header2.h

• Header files can be protected against multiple inclusion:/* header3.h */#ifndef HEADER3_H#define HEADER3_H…#endif

Page 7: 15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.

Compilation and Linking• Before a program can be executed, its source files (not its header files) must be compiled to produce object files. The object files must then be linked.

• Compilation leaves “gaps” that are filled in during linking. For example, a source file may contain calls of functions that are not defined in that file.

• The linker checks that all necessary functions are present (either in a source file or in the library).

• Calls of functions in the C library are resolved by the linker; the compiler knows nothing about these functions.