Splitting program into multiple files?

Posted on 2000-03-01
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 50 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

Will my email signature work in Office 365?

You've built an email signature using raw HTML code in Office 365, but you can't review how it looks with Transport Rules. So you have to test it over and over again before it can be used. Isn't this a bit of a waste of your time? Wouldn't a WYSIWYG editor make it a lot easier?

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to set environment variables in C 2 78
SQL handling single and double quotes 3 94
Can case within switch statement specify range of values ? 3 74
How to learn Linux? 10 41
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now