Splitting program into multiple files?

Posted on 2000-03-01
Medium Priority
Last Modified: 2010-04-15
Hi there,

I would like to take a program I have and split it down into more managable files and use 'make' to compile it.  Currently the program consists of one .c file (fairly big) and one .h file in which all the preprocessor directives are, structure declarations, global variables, etc, etc.

How would I go about splitting this down into multiple files?  What declarations go where? etc, etc..  Basically, any help and pointers you can give me that allows me to break this program up would be appreciated - thanks!!

Question by:aahzman

Expert Comment

ID: 2573606
It's highly dependent on what compiler you're using.

You might start by first breaking the program up into procedures within the same file. ( I assume you have one giant main().)

Then you can transfer each of the procedures to a different .c file.
You will have to tell the compiler and linker which files to use.

Accepted Solution

Iexpert earned 200 total points
ID: 2573616
Generally you group data/functions together in a module
that call each other and then have as small an interface to
this module as possible (as little exported data and functions
as possible).

for e.g. if you were writing a spell checker you could have
the spelling routines in one module and the user interface
routines in another. This can be extended to whatever
extent is practical. For this e.g. you could break the user
interface module into one for GUI and one for console and
each of these would then call the spelling module.

The C language directly supports compiling modules to
obj (object files), and these can then be combined into
library files. Therefore others can use the modules without
having acess to the code, or you can use the binaries to reduce
compilation time (this is because any change to the compilation
unit (c file after including headers) requires recompilation
(hence the name recompilation unit)).

The language details you need for working with modules are:
declare functions and data required within the module only
in the C file, not any header file. Also prefix the declaration
with the static keyword which is overloaded in this context
to mean that these symbols are not visible outside this module.

The parts of the module that you do want to share, declare them
in the header file for the module and use the extern keyword.
Then include this header file in any module that requires it
(including it's own module!!). Note to save pulling your hair out,
for every header file you ever create put the following preprocessor
directives around it:


#endif //HEADER_FILE_H

where HEADER_FILE_H is the actual header file name. This
will cause just one instance of the header file to be included
in each compilation usint which is a requirement.

Also a subtle but important point is in the header files,
include other header files that are required. This use to
cause problems ages ago, but not now with all the automated
tools for cross referencing symbols etc. By doing this you will
remove all include order dependencies and save yourself more

Remember the golden rule for creating functions and modules.
Always try to decrease the coupling and increase the cohesion.


Expert Comment

ID: 2573680
keep all globals and typedefs  in myvars.h
sort all functions to different files like myfile1.c myfile2.c etc etc grouped by meaning or functionalty, make a myfile??.h headers for every that file, then tell the make.exe to use this list for linking, now you can edit every separeted file as part of project... but don't split every line to separated line ;-)

Expert Comment

ID: 2573890
i dont know wat are you waiting .. :o)
chose one all are goods!

Author Comment

ID: 2577393
Thanks very much for the help!


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question