Home » Community » Newbie corner » Global variables in Upp
Global variables in Upp [message #28688] |
Sun, 12 September 2010 03:22  |
jerson
Messages: 202 Registered: June 2010 Location: Bombay, India
|
Experienced Member |

|
|
Earlier http://www.ultimatepp.org/forum/index.php?t=msg&goto=284 87&#msg_28487
I have been able to finally solve the problem in my case. The behaviour is mainly due to the way Upp uses Blitz to compile. I think that behaviour needs to be documented somewhere.
Let us say, my package has 3 files, main.cpp, file1.cpp and file2.cpp All these files have a common Hfile that declares a global variable say gvar. Of course, it has the header locks in place to prevent multiple declarations.
On the first build after IDE is opened, blitz usually builds all the 3 files together and shows like so on the output pane
BLITZ : main.cpp file1.cpp file2.cpp
and the project builds fine.
Finer detail, Blitz compiles it in a single pass like this
Header file
main.cpp (global gets declared here as H file is in here first)
file1.cpp (H file gets locked out)
file2.cpp (H file gets locked out)
Now, if I touch any 1 of the 3 files, only that file gets compiled and the global variable gets declared again in the file which is compiled.
BLITZ file2.cpp (H file gets declared here as this is the only file being compiled now)
Now, during the link phase, the first build case links correctly.
In the second case, the linker has a problem. The header lock does not seem to work. This is because, in case1, main.cpp has the gvar already declared. Now, file2.cpp has gvar declared too since it was compiled all alone by BLITZ. So, I keep getting a multiple definition for gvar from the second build onwards.
How did I solve it? Simple. Declaration of gvar is removed from .H file and put in main.cpp. Now all other Cpp files refer to the variable as extern var.
in main.cpp for example MyStruct gvar;
in file1.cpp extern MyStruct gvar;
in file2.cpp extern MyStruct gvar;
Now, irrespective of how blitz compiles the files, gvar is always in main.cpp and the others know where to find the gvar.
I spent quite a bit of time figuring this out, so I hope my post will help other newbies like me.
Regards
|
|
|
Goto Forum:
Current Time: Sun Apr 27 00:15:47 CEST 2025
Total time taken to generate the page: 0.03419 seconds
|