Re: Regarding use of modules



Paminu wrote:
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

.



Relevant Pages

  • Re: A note on Peter Seebachs vicious little tirade
    ... negative numbers are represented using the two's complement ... Depends what the author meant by Global Variables. ... main may have declarations compatible with those. ... It's not just the proof-reading errors, ...
    (comp.lang.c)
  • Re: managing global variables
    ... > Every file needing access to the global variables #includes this file. ... Clearly there are benefits to having declarations and definitions ... prototypes for ALL functions, ... No extra work to make that happen - just edit source files as usual, ...
    (comp.lang.c)
  • Re: alpha defined: virtualcnt,titleid,keyid,helplineid,pasteboardid
    ... > Where would I best find EXTERN and GLOBAL variables? ... Going back to the C sources: the definitions and (EXTERN) ... thus finding the header file. ... Another way would be to use the CTAGS program, which keeps trace ...
    (comp.os.vms)
  • Re: AfxGetApp() returns NULL
    ... I think that is because many people think of "the global variables" as a lump. ... I find it nonsensical to have a single global header file, ... dissertation, "Program changes and the cost of selective recompilation", ... Note that the scope may be the entire program, but the DEPENDENCY is not! ...
    (microsoft.public.vc.mfc)
  • Re: How to Define a global const class variable in MFC?
    ... Instead I would probably add it to the main app class or perhaps, a class that contains other variables like this that is, itself, hosted in the main app class. ... I try very hard not to use global variables these days and I've found it makes debugging and cleaning up much easier. ... g_Info as int and returns SYNTAX error and redefinition of basic type. ... when I place class definition in a header file and include header file in the ...
    (microsoft.public.vc.mfc)

Loading