Identifier clashes between source files


In C, variables and functions are by default public, so that any C source file may refer to global variables and functions from another C source file. This is true even if the file in question does not have a declaration or prototype for the variable or function. You must, therefore, ensure that the same symbol name is not used in two different files. If you don’t do this you will get
linker errors and possibly warnings during compilation. 

One way of doing this is to prefix public symbols with some string which depends on the source file they appear in. For example, all the routines in gfx.c might begin with the prefix ‘gfx ’. If you are careful with the way you split up your program, use sensible function names, and don’t go overboard with global variables, this shouldn’t be a problem anyway.


To prevent a symbol from being visible from outside the source file it is defined in, prefix its definition with the keyword ‘static’. This is useful for small functions which are used internally by a file, and won’t be needed by any other file.

No comments:

Post a Comment