Advertisement

File Splitting (Externs/Headers)

Started by February 02, 2000 03:42 PM
14 comments, last by Ishamael 24 years, 8 months ago
I have been trying to split up my main.cpp file into several smaller cpp files for a long time. Right now my main.cpp file is over 1300 lines and growing rapidly. This gets really annoying during compilation when I only change one or two lines but it has to look over the ENTIRE file. Anyway, as I said, I''ve been trying to split up my main source file. My problem, though, is this: whenever I #include a header file that contains my variable declarations into two or more source files, I get an error that tells me my variables have been defined twice. How do I get around this? I have seen MANY programs that include a variable declaration file more than once, but have never been able to incorporate such a thing into my own program. Many people have tried helping me with my problem, but nobody has succeeded in informing me of the correct way to access the same variable in two different source files. Can anybody help me? Thanks in advance, Ishamael
It is possible to make sure the compiler does not process the same header file more than once (and have an error stating there are multiple declarations when there is really only one).

At the beginning of the file #define something, then right after that do an #ifndef to check if that thing is defined. At the end of the file include an #endif. If the compiler sees that it has already been defined, the compiler will not compile it again.
Advertisement
You need to add the followng to every header filr you create:

#ifndef filename_h
#define filename_h

// Your code here

#else
#endif

This will protect you from getting those errors.
Another possibility that works is to declare the variables in one cpp-file, and declare it as extern in the header:

something.cpp:
int globalcounter;
something.h:
extern int globalcounter;

You can then include the header file in as many source files as you like. Personally I think this method is better than the #define kludge, because it encourages modular design and forces the disciplined and well thought out use of header files.
Yes, for the problem you want to solve, Gefilus is right. But you should still use include guards to prevent other errors (and time-wasting suring compilation).

-Brian
I''ve had this problem before. The #ifndef/#define/#endif doesn''t seem to prevent that error. What you can do, though, is declare the variables static, which means they only get declared the first time. I''m not sure what kind of draw backs that may have though.

Dare To Think Outside The Box
_____________________________
|____________________________|
http://www.inversestudios.com
Write more poetry.http://www.Me-Zine.org
Advertisement
Gefilus - That method works fine with ints but what about classes? if you say "extern myclass *blah" then the compiler needs a header file for "myclass". So then you have to use the #ifdef etc in myclass.h...
quote:
#ifndef filename_h
#define filename_h

// Your code here

#else
#endif

This is the way we all do it here at work. It works correctly in all instances. FYI, you don''t need the #else directive.
Are u guys sure the #ifndef works? This simple file structure will generate a LNK2005 (Already defined) error on VC++ 6.0

------------------
- Globals.h

#ifndef __GLOBALS_H_
#define __GLOBALS_H_

int Test; // test var

#endif

-----------------

- In Main.cpp
#include "globals.h"
// plus window stuff and winmain

----------------

- In Another_file.cpp
#include "globals.h"

// some other things

-----------------

Unless the VC++ compiler is buggy, I cannot get this to work.

The only way I got it to compile is to create another external file with external declarations of globals.h and the cpp files include the external file instead.
The reason that the above doesnt work is not because VC++ is buggy, but because you are creating two Test variables. The #include directive essentially pastes the header file into your source, so when the compiler gets to your code, it sees this:


----------------- Main.cpp -----------------

int Test; // test var, from globals.h
// plus window stuff and winmain

------------- Another_file.cpp -------------

int Test; // test var, from globals.h also

// some other things

--------------------------------------------


the compiler then makes two variables, both named Test. this is correct to the compiler, but when the linker tries to combine the code together, it sees two integers named Test and gives an error. One correct way to do it right would be to make one Test variable, and then declare at as "extern"al in the rest of the files. To do the above code:


----------------- globals.h ----------------

#ifndef __GLOBALS_H_
#define __GLOBALS_H_

extern int Test; // test var

#endif

----------------- Main.cpp -----------------

#include "globals.h"

int Test; // test var

// other code

------------- Another_file.cpp -------------

#include "globals.h"

// other code

--------------------------------------------


now Test can be used in both files. Usually you define the variable as extern in the included file so all cpp files can see it, and define the actual variable in just one cpp file (usually the one that "owns" it, although really, it doesnt matter)

And having "extern int Test;" followed by "int Test;" won''t cause any problems, the compiler will ignore the extern statement.

Hope this helps (I''m a little tired, likely missed or confused something)

This topic is closed to new replies.

Advertisement