Solved

Splitting program into multiple files?

Posted on 2000-03-01
5
238 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
[X]
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
5 Comments
 
LVL 3

Expert Comment

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

Accepted Solution

by:
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:

#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
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 ;-)
0
 

Expert Comment

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

Author Comment

by:aahzman
ID: 2577393
Thanks very much for the help!

Andy
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

740 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