The C and C++ Programming Languages

Large Projects

Peter L. Peres [plp at ACTCOM.CO.IL] says

The keys to larger C projects are header files and build scripts.

The bug that causes you trouble is a very sane and useful ANSI C feature: Symbols declared in a module are not visible to other modules unless they are declared in the module that uses them as external or as function prototypes.

For example a function int foo(void); shall appear as int foo(void); in the main file, and a global variable int bar shall appear as extern int bar; in same. Of course you don't write them there, you include a library header file instead.

In other words, when making a library, you put the functions in the library, and prototypes for all the public functions in it, and 'external' definitions for all the public data in it, in a corresponding header file (usually called like the libarary but with suffix .h). The IMPORTANT part is that there should be NO actual functions NOR data declarations in the header. Anything not declared in the header will remain private to the libary. However some compilers do not like name clashes between same-named symbols located in different libraries (even if they are not public).

Then in your main program, you #include "path/to/header" and the functions are visible. The library is usually compiled separately or automatically (it's a DLL of sorts at the object file level). You must understand the semantics of the double ticks (as opposed to angled brackets) here, and apply some serious path rewriting for DOS under certain circumstances.

Now you need to tell the compiler about all the files it needs to build the executable. Sometimes you tell this the linker. In other words, pass all the required libraries (already compiled into relocatable object form) to the linker, or pass all the source file names to the compiler. I don't know what compiler you are using but sane (i.e. Unixish) compilers allow you to string everything onto the command line. DOS compilers have a limit on the command line length so they often use a command file which contains the actual list of files to use.

A tool that does almost all of this (the invocation of the toolchain parts) for you is called a make tool and YOU WANT ONE BADLY. There are free ones and there are non free ones. The 'standard' make tool is called... make. See Linux... Another make tool is a batch script of whatever shell you are using (yes, you can do it in DOS if you HAVE to).

Interested:

See:

Comments: