6
CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR

CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR

Embed Size (px)

Citation preview

Page 1: CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR

CCSA 221Programming in CCHAPTER 15

WORKING WITH LARGER PROGRAMS

1ALHANOUF ALAMR

Page 2: CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR

Outline• Dividing Your Program into Multiple Files

• Compiling Multiple Source Files from the Command Line

2

Page 3: CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR

Dividing Your Program into Multiple Files• In every program that you’ve seen so far, it was assumed that the entire

program was entered into a single file and then compiled and executed.

– In this single file, all the functions that the program used were included—except, of course, for the system functions such as printf and scanf. Standard header files such as <stdio.h> and <stdbool.h> were also included for definitions and function declarations.

– This approach works fine when dealing with small programs—that is, programs that contain up to 100 statements or so.

• However, when you start dealing with larger programs, this approach no longer suffices. As the number of statements in the program increases and the time it takes to edit and compile the program also increase.

3

Page 4: CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR

• C supports the notion of modular programming which does not require that all the statements for a particular program be contained in a single file.

• This means that you can enter the code for one module into a file and the code for another module into a different file, and so on.

• Here, the term module refers either to a single function or to a number of related functions that you group them logically.

4

Dividing Your Program into Multiple Files

Page 5: CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR

5

Compiling Multiple Source Files from the Command Line

• Suppose you have divided your program into three modules:1. You have entered the statements for the first module into a file called

mod1.c .2. The statements for the second module into a file called mod2.c and, 3. The statements for your main routine into the file main.c .

• To tell the system that these three modules actually belong to thesame program, you must use the command window to compile the program.

• To open the command window, you have to write cmd in the search box at the start menu.

• At the command window, you must write the names of all three files when you enter the command to compile the program.

• For example, using gcc , the command:$ gcc mod1.c mod2.c main.c –o dbtest

has the effect of compiling the code contained in mod1.c , mod2.c , and main.c.

Page 6: CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR

6

Compiling Multiple Source Files from the Command Line

• Errors discovered in mod1.c , mod2.c , and main.c are separately identified by the compiler. For example, if the gcc compiler gives output that looks like this:

mod2.c:10: mod2.c: In function 'foo':mod2.c:10: error: 'i' undeclared (first use in this function)mod2.c:10: error: (Each undeclared identifier is reported only oncemod2.c:10: error: for each function it appears in.)

As an example, the compiler indicates that mod2.c has an error at line 10, which is in the function foo.

Because no messages are displayed for mod1.c and main.c , no errors are found compiling those modules.