How to declare a global variable?
| Sat, 2005-11-12 22:27 | |
|
Hello everyone,
I'm very new to Symbian C++. I want to declare 'TTime timeOld' to be used as global variable in many classes. If I placed it in header file after the last '#include' (My source code has the only one header file.), the program can run in WINSB emulator with no errors, But It can't be compiled to ARMI. And there are many errors said "multiple definition of 'timeOld'". Can anyone help me please? Thank you in advance. |
|






Forum posts: 17
Regards,
Andrew
Forum posts: 124
Try adding something like this around the content of your header file
#ifndef _ABC_H_
#define _ABC_H_
......
#endif // _ABC_H_
Forum posts: 2
thank you very much for your help.
Regards,
Andrew
I found this in file '.mmp'
TARGETTYPEÂ Â app
Is it impossible to use non-constant global variables in 'app' file?
If it is, how can I change it to 'exe' file? I'm using Borland C++ Mobile Edition.
Or are there any tricks to use global variables in 'app' file?
Try adding something like this around the content of your header file
#ifndef _ABC_H_
#define _ABC_H_
......
#endif // _ABC_H_
I've already added it in between '#ifndef' and '#endif'.
#ifndef __CellLog_H
#define __CellLog_H
.....
TTime timeOld;
.....
#endif
But 'multiple definition' errors still exist.
There are no any errors when compile to WINSB Emulator. And it can be run without any problems.
But why can't it be compiled to ARMI?
Forum posts: 2006
The possible solutions:
- use Symbian OS v9 and UIQ v3 or Series 60 v3: applications are now Exes - not apps anymore - and can have globals.
- use a singleton class to hold all your globals and register in the TLS. (Check the SDL to know what the TLS is and how to use it - it's quite simple).
- design your app so that is uses a UI (the .app) and a server (a .exe) that will hold the globals.
The latter being the most complex and may be not worth if you are new to Symbian.
Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant
Forum posts: 98
In my previous project I had to port some c code that was heavily using global variable.For that i used your Approach 3 And it worked on for all Symbian 7.0 phones.
But i want to know that does symbian 8.0a(on Nokia 6630 and 6680) has different kernal EKA2 so that now DLL can have global data.Is it correct or it has some limitations?
Pl reply
Thanks
Forum posts: 720
If someone used 8.0b or 8.1b, they'd be EKA2 based, but I don't know anybody using them in actual phones.
It isn't until Symbian 9 that only EKA2 is there by default (& the only option). Btw., there are no 9.0 devices, as far as I'm aware, but Nokia's S60 3rd Edition devices are (will be when they ship) 9.1 based.
Forum posts: 98
Now i am interested in following Approach.
" use a singleton class to hold all your globals and register in the TLS. (Check the SDL to know what the TLS is and how to use it - it's quite simple)."
My question is:
In a porting of very large C code that is using global variable heavily. As well as porting developers are not original developer of code so it looks that porting will be very complex and lengthy and it requires well understanding of code also and it does not seem a wise approach in compare to client server Architecture.
Is my conclusion correct .
Plz Correct me if i am wrong.
Thanks
Forum posts: 2006
I have already ported a big piece of C code, not written by me, using several globals - not a lot (~10) but with a lot of reference to them. The port was finally easier than I expected: I redefined the variable names as a macro to access their counterpart in a structure handled by the TLS. This is not very efficient as far as speed is concerned but was enough in my case.
Rewriting the whole application would have been a lot more complex and quite impossible to maintain.
Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant
Forum posts: 3
A massivly simpler way is to simply declare the variables as public in your AppUI object then #define the globals to reference off CEikonEnv::Static()->AppUi() - with appropriate casts of course...
Forum posts: 78
Will CEikonEnv::Static()->AppUi() work on EXE and APP ??
I think we dont have CEikonEnv::Static()->AppUi() in EXEs
So if I am making a DLL library, how can i guess where it going to be used, in APP or EXE
Suggestions Please