Splitting my program to multiple source files.


 
Thread Tools Search this Thread
Top Forums Programming Splitting my program to multiple source files.
# 1  
Old 05-31-2007
Splitting my program to multiple source files.

Hi I have a program, say file1.c which included file1.h, the header file which had lots of #define macros and structure definitions used by file1.c

I have decided to split the source code into 3 separate files (for logical reasons)

file1.c
file2.c
file3.c


Each of these have a corresponding header file which contain only function prototypes:

file1.h (I renamed the earlier file1.h, which had all the #defines and structure defs. to, say, abc.h)
file2.h
file3.h


So, Earlier everything was in file1.c. I removed a set of related functions and put them in file2.c, And another set of related functions and put them in file3.c


file1.c includes...

Code:
#include "file2.h"
#include "file3.h"
#include "abc.h"

file2.c includes..

Code:
#include "file1.h"
#include "abc.h"

file3.c includes..

Code:
#include "file1.h"
#include "abc.h"

Now my question is:

1) What would be an appropriate name for abc.h ?? This has lots of #define macros and structure definitions which is used by all three source files (file1.c, file2.c and file3.c) and hence is #included by all 3 of them.

2) Suppose the original source code had LOTS of global variables. The variables are used by all 3 source files. So how do I deal with them ? Can I put all these global variables in a header file and include it in all the 3 source files ? Will that work ? Or should I leave the global variables in file1.c and then declare them as "extern" in file2.c and file3.c ? (keeping in mind that there are LOTS of them).

3) Can header files contain variable declarations/definitions ???

What happens if I have a header file which has:

int a = 5

and I include this file in a some .c file and use 'a' and change its value ?

Thanks in advance..
# 2  
Old 05-31-2007
1. Make the header file names have some relation to their contents.

2. Dont put global definitions in headers, only put "extern" references to them, I recommend don't put externs in source files.

eg have extern int myFoo,myBar;

3. Only define actual globals once in a source file

Think of it this way....

* A source file is a private implementation file.

* A header file describes the public interface to that implementation.

If you change a global variable, say from an int to a long, then you only have to change it in the specific source it's declared, and the header for that same file, not in a number of other source files.
# 3  
Old 06-01-2007
Hey thanks for your reply.

Quote:
Originally Posted by porter
2. Dont put global definitions in headers, only put "extern" references to them, I recommend don't put externs in source files.
Sorry porter, I did not understand.. should I put global variables in a header file or in a source file ?

Quote:
Originally Posted by porter
If you change a global variable, say from an int to a long, then you only have to change it in the specific source it's declared, and the header for that same file, not in a number of other source files.
So, say in file1.c I have declared a global variable: int x

Then in file2.c I have declared it as extern: extern int x

Now if I change int x to long x, that means I have to change it in multiple files right? file1.c and file2.c
# 4  
Old 06-01-2007
Oh I think I now understand what you mean..

I should declare all global variables in a header file as extern. And define it in only one source file...

Something like this...

file1.c
Code:
#include "globals.h"
int a;
int b;
int c;
main()
{
     a = 1;
     b = 2;
     c = 3;
}

globals.h
Code:
extern int a;
extern int b;
extern int c;

file2.c
Code:
#include "globals.h"
func2()
{
     a++;
     b++;
     c++
}

# 5  
Old 06-01-2007
Quote:
Originally Posted by the_learner
I should declare all global variables in a header file as extern. And define it in only one source file...
Go to the top of the class! Smilie
# 6  
Old 06-01-2007
duplicate post

Last edited by porter; 06-01-2007 at 05:59 PM..
# 7  
Old 06-04-2007
I like using definitions in header's and sources to do this also, like so:

file.h
Code:
#ifndef _FILE_H
#define _FILE_H

#ifdef _FILE_C
#define _FILE_C_CLASS
#else
#define _FILE_C_CLASS extern
#endif

_FILE_C_CLASS int a;
_FILE_C_CLASS int b;

#endif

The top defintion (_FILE_H) prevents recursive includes of the header, common practice.

The next will define all the globals as extern unless _FILE_C is defined. Of course, in file.c you'd define that before including file.h as follows:

file.c
Code:
#define _FILE_C
#include "file.h"

/* source */

#undef _FILE_C

This way I know that one and only one C file (the one that defined _FILE_C, which should be file.c) contains the definitions of all the globals.

Meh...just my personal style that I picked up...sometimes I'll do this too:

file.h
Code:
_FILE_C_CLASS int a
#ifdef _FILE_C
= 5
#endif
;

This is probably arguable whether or not it is good, because it tells the "user" including the header what the initial value of the variable is, which you may want to hide.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script for splitting file of records into multiple files

Hello I have a file of following format HDR 1234 abc qwerty abc def ghi jkl HDR 4567 xyz qwerty abc def ghi jkl HDR 890 mno qwerty abc def ghi jkl HDR 1234 abc qwerty abc def ghi jkl HDR 1234 abc qwerty abc def ghi jkl -Need to split this into multiple files based on tag... (8 Replies)
Discussion started by: wincrazy
8 Replies

2. Shell Programming and Scripting

Splitting file into multiple files and renaming them

Hi all, Newbie here. First of all, sorry if I made any mistakes while posting this question in terms of rules. Correct me if I am wrong. :b: I have a .dat file whose name is in the format of 20170311_abc_xyz.dat. The file consists of records whose first column consists of multiple dates in... (2 Replies)
Discussion started by: chanduris
2 Replies

3. Shell Programming and Scripting

Splitting a single file to multiple files

Hi Friends , Please guide me with the code to extract multiple files from one file . The File Looks like ( Suppose a file has 2 tables list ,column length may vary ) H..- > File Header.... H....- >Table 1 Header.... D....- > Table 1 Data.... T....- >Table 1 Trailer.... H..-> Table 2... (1 Reply)
Discussion started by: AspiringD
1 Replies

4. UNIX for Dummies Questions & Answers

Splitting up a text file into multiple files by columns

Hi, I have a space delimited text file with multiple columns 102 columns. I want to break it up into 100 files labelled 1.txt through 100.txt (n.txt). Each text file will contain the first two columns and in addition the nth column (that corresponds to n.txt). The third file will contain the... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Dummies Questions & Answers

Splitting multiple files in a folder

Hello, I am new to UNIX etc and am trying to split a large number of files, all with the extension .fas in the same folder, into smaller files wherever a string of 5ns occurs. So this file: >File1.fas nnnnnaaaaaattgattttctcagtatcgacgaatatggcgcagaaagttgaataa ... (1 Reply)
Discussion started by: Bryony
1 Replies

6. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Shell Programming and Scripting

splitting a file (xml) into multiple files

To split the files Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Test.xml --------- <?xml version="UTF_8"> <emp: ....> <name>a</name> <age>10</age> </emp> <?xml version="UTF_8"> <emp: ....> <name>b</name> <age>10</age>... (11 Replies)
Discussion started by: sasi_u
11 Replies

8. Shell Programming and Scripting

help splitting a file into multiple files in bash

I have a file that logs multiple sessions. What I would like to do is split this file inclusive of the lines that include "starting session" and "shutting down" and ignore the data before and after the beginning of the first session and the end of the last session. The output files can be called... (2 Replies)
Discussion started by: elinenbe
2 Replies

9. Shell Programming and Scripting

splitting huge xml into multiple files

hi all i have a some huge html files (500MB to 1GB). Each file has multiple <html></html> tags <html> ................. .................... .................... </html> <html> ................. .................... .................... </html> <html> .................... (5 Replies)
Discussion started by: uttamhoode
5 Replies

10. Shell Programming and Scripting

Splitting input files into multiple files through AWK command

Hi, I needs to split *.txt files from single directory depends on the some mutltiple input values. i have wrote the code like below for file in *.txt do grep -i -h "value1|value2" $file > $file; done. My requirment is more input values needs to be given in grep; let us say 50... (3 Replies)
Discussion started by: arund_01
3 Replies
Login or Register to Ask a Question