gcc and Static Variables

F

fewgoodmen

I am currently working on changing the compiler from Sun Workshop to
g++ for a mid sized C program. In the C files we have static variables
defined and declared. This works fine with the Sun Workshop compiler.
However under g++ the static variables are initialized to zero but do
not have the value they are assigned to. For example say file2.cc I
have static variable defined and declared as
static int j = 5;
Variable j seems to have a value of zero and not 5. However any static
variables defined and declared in the file that contains function main
seems to have the value that is assigned to them. I am using g++
version 3.2.2 in a Solaris 8 environment.
Any help would be appreciated.

Joe
 
R

Richard Bos

I am currently working on changing the compiler from Sun Workshop to
g++ for a mid sized C program. In the C files we have static variables
defined and declared. This works fine with the Sun Workshop compiler.
However under g++ the static variables are initialized to zero but do
not have the value they are assigned to. For example say file2.cc I
have static variable defined and declared as
static int j = 5;

File-scope static, or block-scope static?
Variable j seems to have a value of zero and not 5. However any static
variables defined and declared in the file that contains function main
seems to have the value that is assigned to them.

Since the C compile works, and the C++ one does not, this seems to me to
be a problem of C++, not of C, and to belong in comp.lang.c++, not here.
In any case, the line as you quote it should indeed result in the object
being initialised to 5; as far as this group is concerned, it is
correct.

Richard
 
C

CBFalconer

I am currently working on changing the compiler from Sun Workshop
to g++ for a mid sized C program. In the C files we have static
variables defined and declared. This works fine with the Sun
Workshop compiler. However under g++ the static variables are
initialized to zero but do not have the value they are assigned
to. For example say file2.cc I have static variable defined and
declared as
static int j = 5;
Variable j seems to have a value of zero and not 5. However any
static variables defined and declared in the file that contains
function main seems to have the value that is assigned to them.
I am using g++ version 3.2.2 in a Solaris 8 environment.
Any help would be appreciated.

Sounds like something else wrong. For example, you should be using
gcc for C programs, because g++ primarily alters the default
libraries used. g++ is for C++, not C. At the same time recognize
how gcc identifies the language, i.e. (unless specifically told
otherwise) by the source extension. .C or .cpp identifies C++,
while .c identifies C.

At any rate this is off-topic for c.l.c, as is anything that is
system or compiler specific. You should look for a newsgroup
dealing with gnu and/or gcc.

Since you are using the broken google groups system, note the info
in my sig below.
 
F

Flash Gordon

I am currently working on changing the compiler from Sun Workshop to
g++ for a mid sized C program.

g++ is a C++ compiler, not a C compiler. gcc is the C compiler.
> In the C files we have static variables
defined and declared. This works fine with the Sun Workshop compiler.
However under g++ the static variables are initialized to zero but do
not have the value they are assigned to. For example say file2.cc I

Files ending in .cc are normally C++ source files, not C source files.
Although this convention is not mandated by the standard. This news
group only does C, not C++ which is covered just down the hall in
comp.lang.c++
have static variable defined and declared as
static int j = 5;
Variable j seems to have a value of zero and not 5. However any static
variables defined and declared in the file that contains function main
seems to have the value that is assigned to them. I am using g++
version 3.2.2 in a Solaris 8 environment.
Any help would be appreciated.

First decide if it is a C or C++ you are working with, then select the
appropriate group and compiler. Then provide to the appropriate group a
minimum compilable program exhibiting your problem and see what people say.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top