Prev | Next |
Breaking a program into modules | ||||
---|---|---|---|---|
Programs are usually build from several modules. You typically break each module into two files:
If there is a function in the code file that other files should be able to use, then there should be a prototype of that function in the header file. If there is a variable in the code file that other files should know about, then there is an extern declaration in the header file. Here is an example of a pair of files. They must be in two different files. These should not be taken as a recommendation on how to write functions: realistically, char_to_count should be a parameter to function count_chars.
When you compile your files, you produce object files, with extension .o or .obj. (This use of the term "object" is unrelated to the use of the same term in object-oriented programming.) You then run a linker to link the object files together into a single executable file.
|
The main program |
---|
When the linker builds an executable file, it looks for a function of a specific, and creates the executable file so that it starts with that function. In most systems, the function to start with is called main. The main function has three parameters, although you often use only the first two. When you use two, your main program heading looks like this. int main(int argc, char* argv[])where array argv holds the parts of the command line that was used to invoke this program, and argc is the number of members of argv. argv[0] is the name of the command. If you type command myprog -d test.txtthen argc is 3 and argv is as follows. argv[0] = "myprog" argv[1] = "-d" argv[2] = "test.txt"The result returned by main is the exit status of the program. Normally, it is 0. A nonzero value indicates that an error occurred during the program's execution. If you do not use the parameters argc and argv, you can omit them, and write int main() |
Building software | ||
---|---|---|
Integrated development environments provide features, usually called projects, for linking several modules together into a single executable program. An approach to the same thing that is more versatile, and that does not require an integrated development environment, is a makefile. A makefile gives information telling how each part of a system depends on other parts, and tells how to build the system. The following is a sample makefile. It builds an executable program called go from two modules, main.cc and stuff.cc.
The makefile defines variables (COMPILE, etc) that stand for strings. In the body, $(COMPILE) is replaced by g++. The makefile defined targets main, stuff.o, main.o and clean. Each target is followed, on the same line, by a list of the things that target depends on. If any of those things are modified, then this target must be rebuilt. After that is a line (or lines) beginning with a tab, giving a command (or commands) that will build the target. To build main, you type command make mainMake will run all of the necessary commands. The target clean is not really something to be built. Running it removes all of the machine-generated files. If you want to rebuild everyting, you type make clean make main |
Prev | Next |