[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

656 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