GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-May-2005, 05:30
bithead bithead is offline
New Member
 
Join Date: May 2005
Posts: 2
bithead is on a distinguished road
Question

Multiple modules compilation: begginer's question


Hi all,

I am new to C and I would like to ask the following question:

Assume we have these 3 files:
-----------------
alpha.h
-----------------
CPP / C++ / C Code:
struct point
{
   int x;
   int y;
};

int a;

-----------------
beta.c
-----------------
CPP / C++ / C Code:
#include "alpha.h"

//...... (some code) .....

----------------
gamma.c
----------------
CPP / C++ / C Code:
#include "alpha.h"

//..... ( some code ) .....

These three files are supposed to be compiled and then linked into one executable. My question is: isn't it a problem that both beta.c and gamma.c include the same header file (alpha.h)? Since alpha.h defines a struct and an int, both beta.c and gamma.c will include the same definition. Won't that be a problem for the linker? Shouldn't a variable be defined in one place only and then declared in other modules with the extern keyword?

Thanks!
Last edited by LuciWiz : 25-May-2005 at 05:38. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 25-May-2005, 07:17
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello bithead and welcome to GIDForums™.

You are correct. When you include a file into several source files that you will compile you need to be careful.

The structure in this case is simply defining a type of variable and is not declaring a variable in and of itself.

The int declaration, on the other hand, is declaring a variable and will cause you some grief. Besides the obvious reasons not to use global variables, this is another reason to avoid them when possible.

If you must use global variables, define them as extern and then define them normally in only one of your modules. All of your modules will have access to the variable, but it will only be defined once.
  #3  
Old 25-May-2005, 12:59
bithead bithead is offline
New Member
 
Join Date: May 2005
Posts: 2
bithead is on a distinguished road
Thank you! That makes sense.
  #4  
Old 25-May-2005, 19:15
ubergeek ubergeek is offline
Awaiting Email Confirmation
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
you can also put "inclusion guards" in your header file:
CPP / C++ / C Code:
//alpha.h
#ifndef _ALPHA_H_ //"if macro _ALPHA_H_ has not been defined..."
#define _ALPHA_H_ //"...define it"

struct mystruct
{
int a;
float b;
char c;
};
int g_var;

#endif //always close your #if*s. This is like a closing bracket on if(){}


//alpha1.cpp
#include "alpha.h"
...some code


//alpha2.cpp
#include "alpha.h"
...some code

Can you see how those two preprocessor directives at the top make it so that the code in the header file is only compiled once, no matter how many times alpha.h is included? (code between #if/#ifdef/#ifndef and #endif is only compiled if the condition specified in the initial #if* statement is satisfied.)
Last edited by ubergeek : 25-May-2005 at 19:15. Reason: whoops! always end structs with a semicolon...
  #5  
Old 25-May-2005, 19:39
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by ubergeek
you can also put "inclusion guards" in your header file:
That only prevents multiple inclusion within the same translation unit. It does nothing to ensure only one definition from one translation unit to the next. (Not that such sentinels are in any way a bad idea.)

If a variable is meant to be available whenever the header is needed, it is perhaps better to extern it there.
CPP / C++ / C Code:
//alpha.h
#ifndef ALPHA_H_ // leading underscores are reserved for the implementation
#define ALPHA_H_

struct mystruct
{
   int a;
   float b;
   char c;
};

extern int g_var; /* declaration */

#endif
CPP / C++ / C Code:
//alpha1.cpp
#include "alpha.h"
int g_var; /* definition (implicitly initialized to zero) */
/* ... */
CPP / C++ / C Code:
//alpha2.cpp
#include "alpha.h"
/* code "knows about" g_var */
/* ... */
  #6  
Old 25-May-2005, 20:09
ubergeek ubergeek is offline
Awaiting Email Confirmation
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
sorry dave, what is a "translation unit"?
  #7  
Old 25-May-2005, 20:15
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by ubergeek
sorry dave, what is a "translation unit"?
A trivial definition might be "module" or "source file", but it really goes deeper into the final thingy that includes (no pun inteded) all of what is included by the module or source file -- headers included by headers, etc. What the source file would look like after preprocessing.

[edit]Ooh -- I winged it pretty good.

Quote:
Originally Posted by C99
A C program need not all be translated at the same time. The text of the program is kept in units called source files, (or preprocessing files) in this International Standard. A source file together with all the headers and source files included via the preprocessing directive #include is known as a preprocessing translation unit. After preprocessing, a preprocessing translation unit is called a translation unit. Previously translated translation units may be preserved individually or in libraries. The separate translation units of a program communicate by (for example) calls to functions whose identifiers have external linkage, manipulation of objects whose identifiers have external linkage, or manipulation of data files. Translation units may be separately translated and then later linked to produce an executable program.
I've translated the standard's use of italics to indicate definitions of terms and used underlining instead. (Something strange with to do with a quote box?)
  #8  
Old 25-May-2005, 21:36
ubergeek ubergeek is offline
Awaiting Email Confirmation
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
ok thx i get it now. yes all quotes are italicized
 
 

Recent GIDBlogRunning Linux Programs at Boot Time by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linker errors with multiple file progam nkhambal C Programming Language 2 24-Apr-2005 02:37

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:15.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.