Solved

Splitting program into multiple files?

Posted on 2000-03-01
5
229 Views
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!!

Andy
0
Comment
Question by:aahzman
5 Comments
 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
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.
0
 
LVL 3

Accepted Solution

by:
Iexpert earned 50 total points
Comment Utility
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:

#ifndef HEADER_FILE_H
#define HEADER_FILE_H

#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
hair.

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

pixelbeat.
0
 
LVL 3

Expert Comment

by:Alisher_N
Comment Utility
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 ;-)
0
 

Expert Comment

by:ColmaN
Comment Utility
i dont know wat are you waiting .. :o)
chose one all are goods!
regards
0
 
LVL 1

Author Comment

by:aahzman
Comment Utility
Thanks very much for the help!

Andy
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and reading files in the C programming language.

744 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