How to declare a global variable?

Login to reply to this topic.
Sat, 2005-11-12 22:27
Joined: 2005-11-12
Forum posts: 2
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.

Sun, 2005-11-13 11:31
Joined: 2005-09-05
Forum posts: 17
Re: How to declare a global variable?
Hi! The fact that you have only one header doesn't matter. It really depends on number of source files you include it in. And non-constant global variables are NOT permitted in Symbian unless it is .exe file.

Regards,
Andrew
Sun, 2005-11-13 12:31
Joined: 2003-10-10
Forum posts: 124
Re: How to declare a global variable?
I think the fact that you are getting a "multiple definition" warning suggests that your header file doesnt have include guards in it which means that each time the header file is included in a CPP file it will try to include the definitions again.

Try adding something like this around the content of your header file

#ifndef _ABC_H_
#define _ABC_H_

......


#endif // _ABC_H_
Mon, 2005-11-14 06:02
Joined: 2005-11-12
Forum posts: 2
Re: How to declare a global variable?
Hello everybody,

thank you very much for your help.

Quote from: LordOsh
Hi! The fact that you have only one header doesn't matter. It really depends on number of source files you include it in. And non-constant global variables are NOT permitted in Symbian unless it is .exe file.

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?  Wink

Quote from: navaron
I think the fact that you are getting a "multiple definition" warning suggests that your header file doesnt have include guards in it which means that each time the header file is included in a CPP file it will try to include the definitions again.

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?
Mon, 2005-11-14 09:32
NewLC AdministratorSymbian AccreditedForum Nokia Champion
Joined: 2003-01-14
Forum posts: 2006
Re: How to declare a global variable?
Quote
Is it impossible to use non-constant global variables in 'app' file?
Yes, it's impossible.

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

Mon, 2005-11-14 14:25
Joined: 2005-02-12
Forum posts: 98
Re: How to declare a global variable?
Hi Eric,
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

Mon, 2005-11-14 17:13
Forum Nokia Champion
Joined: 2003-06-10
Forum posts: 720
Re: How to declare a global variable?
The 8.0a based phones use the same old EKA1 (EPOC Kernel Architecture) as earlier Symbian versions. Same with 8.1a.

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.
Thu, 2005-11-17 11:37
Joined: 2005-02-12
Forum posts: 98
Re: How to declare a global variable?
Thanks for promt reply
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
Thu, 2005-11-17 14:12
NewLC AdministratorSymbian AccreditedForum Nokia Champion
Joined: 2003-01-14
Forum posts: 2006
Re: How to declare a global variable?
Actually the answer is "it depends".

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

Thu, 2005-11-17 17:33
Joined: 2005-11-17
Forum posts: 3
Re: How to declare a global variable?
Depends on exactly your circumstances but normally creating your own TLS or a separate process to hold globals is a bit over the top....

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...
Thu, 2006-06-15 18:58
Joined: 2006-04-17
Forum posts: 78
Re: How to declare a global variable?

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
  • Login to reply to this topic.