![]() |
|
#1
|
|||
|
|||
Multiple modules compilation: begginer's questionHi 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:
----------------- beta.c ----------------- CPP / C++ / C Code:
---------------- gamma.c ---------------- CPP / C++ / C 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
|
||||
|
||||
|
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. __________________
The best damn Sports Blog period. |
|
#3
|
|||
|
|||
|
Thank you! That makes sense.
|
|
#4
|
|||
|
|||
|
you can also put "inclusion guards" in your header file:
CPP / C++ / C 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
|
|||
|
|||
|
Quote:
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:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
|
sorry dave, what is a "translation unit"?
|
|
#7
|
|||
|
|||
|
Quote:
[edit]Ooh -- I winged it pretty good. Quote:
|
|
#8
|
|||
|
|||
|
ok thx i get it now. yes all quotes are italicized
|
Recent GIDBlog
Running Linux Programs at Boot Time by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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