Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static globals in local include files

Status
Not open for further replies.

gopet

Programmer
Oct 6, 2003
6
GR
Hello everybody,

in an example project with VC6.0

config.h
config.cpp

main.cpp

I am using some global variables in `config.h'.
If i won't declare them as `static' then the conpiler stops when compiling with
critical error; telling me something about undercleared/redeclared symbols in .obj files. I understand i have to declare them as static if i want it to compile but the thing is that in programs i have downloaded (src only from the net) other people does not declare those vars as static.

Is there a compiler switch or something for this?
 
Please put here a byte of your code to give you some right feedback.
-obislavu-
 
! wow very quick, thanks
ok here is a simple example and the compiler output
in the end

Code:
/* config.h */

#ifndef CONFIG_H
#define CONFIG_H

#include <iostream>

int WhatIsWrong; /* THIS SHOULD BE STATIC */
void SayHelloToTheWorld(char *s);

#endif

/* config.cpp */

#include &quot;config.h&quot;

void SayHelloToTheWorld(char *s)
{
	std::cout << &quot;i have to say: &quot; << s << std::endl;
}

/* new.cpp */

#include &quot;config.h&quot;

void main() 
{
	SayHelloToTheWorld(&quot;Hello World!&quot;);	
}

/* COMPILER VC6 SAYS: */

-------Configuration: hello - Win32 Debug----
Compiling...
new.cpp
config.cpp
Linking...
config.obj : error LNK2005: &quot;int WhatIsWrong&quot; (?WhatIsWrong@@3HA) already defined in new.obj
Debug/hello.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

hello.exe - 2 error(s), 0 warning(s)
 
It is not recomended to declare instance of objects in the .h file.
Your project is not compiling because there are two definitions of the int WhatIsWrong generated in the .obj files.
It will work only if you declare it static but you should move it in a .cpp file or wrap it in a structure using typedef or in a class and leave it in the .h file.

-obislavu-





 
Some addition: you may declare var as extern in .h file and place its definition (w/o extern) in main.cpp, for example. If you add static to var declaration you can't access this (module scope) var from other modules (files). Read about declaration and definition of an entity in any good book about C or C++.
Declare global vars is not a good practice. Avoid globals - I never need any globals for 25 years (in PL/I, C and C++). We always may have the better solution, especially in C++.
 
Well thanks everybody.

Yes it's no good to have globals in a program i know.
I hope i won't say anything foolish, but i see win32api
fully procedural /* ok with classes but procedural */.

Thus writing a simple win32 program with 2 or 3 windows you have to declare HINSTANCEs and Handles to control your windows. I don't know how to `don't' do it /* use globals */.

Finally i found out that the correct code whould be:

Code:
/* config.h */

#ifndef CONFIG_H
#define CONFIG_H

#include <iostream>

extern int WhatIsWrong;
void SayHelloToTheWorld(char *s);

#endif

/* config.cpp */

#include &quot;config.h&quot;

void SayHelloToTheWorld(char *s)
{
    std::cout << &quot;i have to say: &quot; << s << std::endl;
}

/* new.cpp */

#include &quot;config.h&quot;

int WhatIsWrong;

void main()
{

    WhatIsWrong = 0;
    SayHelloToTheWorld(&quot;Hello World!&quot;);    
    std::cout << &quot;WhatIsWrong: &quot; << WhatIsWrong << std::endl;

}

/* with final output */

i have to say: Hello World!
WhatIsWrong: 1
Press any key to continue

------

petros is happy he understood it :)


PS. ArkM: Yes i whould be very happy if i new how to write good oop without globals, etc,etc , but i can't :) I hope over the years...
 
It shall be as you wish, gopet. Good luck!
 
am i wrong or something?

i don't mean any disrespect
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top