Paminu said:
I am trying to split my program in different parts.
I have a file called mainfile.c that contains the main() function, some
global variables and a few other functions.
I then have a file called mainfile.h that contains some structs and a list
of the functions that are used in mainfile.c
I am then trying to make another file called my_func.c that contains some
other functions that when called will update some of the global variables
defined in mainfile.c. I guess the functions that I implement here needs to
be mentioned in mainfile.h. But what about the global variables should the
not be moved from mainfile.c to mainfile.h?
What you are doing is learning rules for effective modularization.
The compiler requires that all relevant information be available when a
module is compiled. When there is information which more than one
module needs, such as declarations for functions, structures, and global
variables, a recommended way to achieve this is with a header file that
is included in multiple code files as needed.
Here is how I organize my code and header files:
A code file usually contains a function or set of related functions,
such as those needed to drive an LCD. The file might be named lcd.c and
have several functions needed to control the LCD. It may have other
functions as well that are used by the various LCD routines, such as
checking LCD status. The functions that are called by other modules
have prototypes in an associated header file, lcd.h. The functions that
are only referenced internally do not have prototypes in lcd.h. They
might have prototypes in lcd.c or I may simply define the functions
before they are referenced, so that no separate prototype is unneeded.
These internal functions are declared static to prevent access from
outside the lcd module.
The header file associated with the code file (lcd.h in this example)
contains all the declarations and information needed to use lcd.c. It
literally defines the interface for those functions. This includes a
fair amount of comments preceding each function prototype. You
shouldn't need to read the code file to know what it does.
When I have global variables (which most programmers try to minimize),
they are declared in a header file, which is included by any module
referencing. It usually is declared in header file for the module most
responsible for the variable.
I sometimes have header files not associated with code files that define
project-wide parameters or types.
These rules are my requirements, beyond what is required by the
compiler, which help me organize my code for my own understanding.
The book The Pragmatic Programmer contains a recommendation called the
DRY principle -- Don't Repeat Yourself -- which means defining something
in only one place. In this case, it is the declarations of the
functions and global variables. When they are defined in only one
place, you don't have consistency problems, which can happen if
something is defined in multiple places and then changed later,
erroneously only in some, but not all places. The principle applies to
many other areas of programming, as well, such as application-related
constants.
A major challenge in programming is managing complexity. One of the
most effective techniques is good modularization. Others, of course,
will suggest additional or replacement concerns. ;-)
Thad