469,898 Members | 1,707 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

enum variables and extern

I have a file, data.c, where I define all of my global variables.

I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.

For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:

enum myenum
{
Mon,
Tues,
Wed
} enum_var;

Now when I try to reference enum_var in data.h, the compiler gives me
issues. I have tried multiple ways of referencing it and defining it,
but nothing is working. I guess I just don't understand something
about enum types and/or extern.

Any help would be great.

Thanks,

Charlie

Apr 30 '07 #1
12 21176
Charlie wrote:
I have a file, data.c, where I define all of my global variables.

I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.

For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:

enum myenum
{
Mon,
Tues,
Wed
} enum_var;

Now when I try to reference enum_var in data.h, the compiler gives me
issues. I have tried multiple ways of referencing it and defining it,
but nothing is working. I guess I just don't understand something
about enum types and/or extern.
You need to define 'enum myenum' everywhere you reference it. You
can't use an enum by merely declaring it, as you can for variables. So
it has to be in the data.h file.

data.c:
#include "data.h"
enum myenum enum_var;

data.h:
extern enum myenum
{ ... } enum_var;

or

data.h:
enum myenum
{ ... };
extern enum myenum enum_var;

Apr 30 '07 #2
Charlie wrote:
I have a file, data.c, where I define all of my global variables.

I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.

For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:

enum myenum
{
Mon,
Tues,
Wed
} enum_var;

Now when I try to reference enum_var in data.h, the compiler gives me
issues. I have tried multiple ways of referencing it and defining it,
but nothing is working. I guess I just don't understand something
about enum types and/or extern.


/* a.h */
enum myenum { Mon, Tues, Wed };
enum myenum fun(enum myenum x);
/* a.c */
#include <stdio.h>
#include "a.h"

int main(void)
{
enum myenum y = Mon;
printf("Calling fun with y = %d\n", y);
y = fun(y);
printf("Now y = %d\n", y);
return 0;
}
/* b.c */
#include "a.h"

enum myenum fun(enum myenum x)
{
return x + 1;
}
Apr 30 '07 #3
Charlie wrote On 04/30/07 10:35,:
I have a file, data.c, where I define all of my global variables.
[...]
Perhaps you should explain why you are dissatisfied
with the answers you got when you posted this same question
a week ago. Otherwise, chances are you'll get the same
answers all over again and be no further ahead than you
are right now.

--
Er*********@sun.com
Apr 30 '07 #4
Charlie wrote:
I have a file, data.c, where I define all of my global variables.

I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.

For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:

enum myenum
{
Mon,
Tues,
Wed
} enum_var;

Now when I try to reference enum_var in data.h, the compiler gives me
issues. I have tried multiple ways of referencing it and defining it,
but nothing is working. I guess I just don't understand something
about enum types and/or extern.
We did this last week (or was it the week before). Have a look in the
archives.

Briefly: you have to declare your enum type in your data.h file.

It worries me that you have so many global variables you have "all"
of them, and all in one place too.

--
"How am I to understand if you won't teach me?" - Trippa, /Falling/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Apr 30 '07 #5
On Apr 30, 10:59*am, Harald van Dijk <true...@gmail.comwrote:
Charlie wrote:
I have a file, data.c, where I define all of my global variables.
I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.
For the most part, I am having no problem with this. *However, I
defined an variable of type myenum in data.c:
enum myenum
{
* * Mon,
* * Tues,
* * Wed
} enum_var;
Now when I try to reference enum_var in data.h, the compiler gives me
issues. *I have tried multiple ways of referencing it and definingit,
but nothing is working. *I guess I just don't understand something
about enum types and/or extern.

You need to define 'enum myenum' everywhere you reference it. You
can't use an enum by merely declaring it, as you can for variables. So
it has to be in the data.h file.

data.c:
#include "data.h"
enum myenum enum_var;

data.h:
extern enum myenum
{ ... } enum_var;

or

data.h:
enum myenum
{ ... };
extern enum myenum enum_var;
Thank you for the speedy response.

However, when I define myenum in data.h, for every file I include
data.h, I get the compiler error:

Error: identifier myenum redeclared in data.h

Note: I am removing the myenum declaration in data.c.

On top of that, I also get:

Error: illegal name overloading

For the first two identifier within myenum { ... } (there are three)
everytime I get the redeclaration error.

I am really confused.

Apr 30 '07 #6
On Apr 30, 11:16 am, Eric Sosman <Eric.Sos...@sun.comwrote:
Charlie wrote On 04/30/07 10:35,:
I have a file, data.c, where I define all of my global variables.
[...]

Perhaps you should explain why you are dissatisfied
with the answers you got when you posted this same question
a week ago. Otherwise, chances are you'll get the same
answers all over again and be no further ahead than you
are right now.

--
Eric.Sos...@sun.com
Sorry, I apologize for reposting it. I posted it last week and it
never showed up, as far as I could see. I sent a request to google to
fix it and never received a reponse. This morning I did do a search
for it and nothing came up so I assumed it never went through. So I
just tried to post it again. I will again search for the question to
see the responses I got there as well.

Again, I apologize

Charlie

Apr 30 '07 #7
On Apr 30, 11:37 am, Charlie <evilzucch...@yahoo.comwrote:
On Apr 30, 11:16 am, Eric Sosman <Eric.Sos...@sun.comwrote:
Charlie wrote On 04/30/07 10:35,:
I have a file, data.c, where I define all of my global variables.
[...]
Perhaps you should explain why you are dissatisfied
with the answers you got when you posted this same question
a week ago. Otherwise, chances are you'll get the same
answers all over again and be no further ahead than you
are right now.
--
Eric.Sos...@sun.com

Sorry, I apologize for reposting it. I posted it last week and it
never showed up, as far as I could see. I sent a request to google to
fix it and never received a reponse. This morning I did do a search
for it and nothing came up so I assumed it never went through. So I
just tried to post it again. I will again search for the question to
see the responses I got there as well.

Again, I apologize

Charlie
My mistake was that I sorted by relevance and not by date. . . it
comes up in date.

First, I am not first and foremost a programmer. I did a little PHP a
few years ago but mainly design hardware and have been doing VHDL and
schematic capture for the past 7 years. I recently started this new
job and, it being a small company, when a new project came along my
boss said "hey, you have some programming experience, so you are in
charge of doing this embedded application."

So I got thrust back into the world of programming. So my style is
going to be bit rough around the edges because I am used to thinking
in parallel and now i have to think of everything sequentially. My
apologies for sounding like an unrefined noob.

I am working on an embedded processor where I am dealing with a lot of
different interrupts. From my hardware days, it always made sense to
make a flag or enable that would trigger different parts of my
design. I am not sure how to do that in C without global variables.

I am more than willing to take any suggestions or criticisms because I
would really like to refine my skills.

Thanks agian,

Charlie

Apr 30 '07 #8
Harald van Dijk <tr*****@gmail.comwrites:
Charlie wrote:
>I have a file, data.c, where I define all of my global variables.

I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.

For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:

enum myenum
{
Mon,
Tues,
Wed
} enum_var;

Now when I try to reference enum_var in data.h, the compiler gives me
issues. I have tried multiple ways of referencing it and defining it,
but nothing is working. I guess I just don't understand something
about enum types and/or extern.

You need to define 'enum myenum' everywhere you reference it. You
can't use an enum by merely declaring it, as you can for variables. So
it has to be in the data.h file.
That's a little bit misleading. The original declaration:

enum myenum { Mon, Tues, Wed } enum_var;

is perfectly valid by itself. It does two things: it declares a type
called "enum myenum", and *defines* an object of that type called
"enum_var".

Being able to define a type and an object of that type together in a
single declaration can be convenient, but it's a feature that's
usually best avoided. The problem here is that it's in a header,
which means it's likely to be seen multiple times in the same program,
once for each translation unit that includes the header. That's ok
for the type declaration, but it's not ok for the variable
declaration; it attempts to create multiple variables of the same
name, which is why you get the error message.

The way to fix it is to declare just the type in the header, and to
define the object in a ".c" file. You can *declare* the object in the
header (if you must), but you shouldn't *define* it there.

data.h:
enum myenum { Mon, Tues, Wed };
extern enum myenum enum_var;

data.c:
enum myenum enum_var;

The "extern" means it's a declaration but not a definition; it doesn't
create the variable, it merely declares that it exists elsewhere.

This all assumes that you really want a global variable. Perhaps you
do, but it's very likely that you don't.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 1 '07 #9
Charlie wrote:
>
I have a file, data.c, where I define all of my global variables.

I then have a header file, data.h, which I include in every file
in which I reference all of the variables defined in data.c.

For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:

enum myenum
{
Mon,
Tues,
Wed
} enum_var;

Now when I try to reference enum_var in data.h, the compiler
gives me issues. I have tried multiple ways of referencing it
and defining it, but nothing is working. I guess I just don't
understand something about enum types and/or extern.
Just like any other type. Define the type in the header file:

enum myenum {Mon, Tues, Wed}; /* note no object */

along with an extrn reference for an object:

extrn enum myenum enum_var;

That handles the include file, which you include in each .c file
that references enum_var. Now each place where it is included knows
what it is, and that it is extrn.

In just ONE of those files you have the object declaration (which
overides the extrn):

enum myenum enum_var;

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 1 '07 #10
Charlie wrote:
On Apr 30, 10:59*am, Harald van D?k <true...@gmail.comwrote:
>Charlie wrote:
I have a file, data.c, where I define all of my global variables.
I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.
For the most part, I am having no problem with this. *However, I
defined an variable of type myenum in data.c:
enum myenum
{
Mon,
Tues,
Wed
} enum_var;
Now when I try to reference enum_var in data.h, the compiler gives
me issues. *I have tried multiple ways of referencing it and
defining it, but nothing is working. *I guess I just don't
understand something about enum types and/or extern.

You need to define 'enum myenum' everywhere you reference it. You
can't use an enum by merely declaring it, as you can for variables.
So it has to be in the data.h file.

data.c:
#include "data.h"
enum myenum enum_var;

data.h:
extern enum myenum
{ ... } enum_var;

or

data.h:
enum myenum
{ ... };
extern enum myenum enum_var;

Thank you for the speedy response.

However, when I define myenum in data.h, for every file I include
data.h, I get the compiler error:

Error: identifier myenum redeclared in data.h

Note: I am removing the myenum declaration in data.c.

On top of that, I also get:

Error: illegal name overloading

For the first two identifier within myenum { ... } (there are three)
everytime I get the redeclaration error.

I am really confused.
Unfortunately, we have too little information to properly diagnose the
cause of this problem.
Can you post the smallest complete program that generates these error
messages?
Please copy/paste the code, and leave nothing out. If the problem is not
something we have seen a dozen times before, it helps if we can run the
program through our own compilers.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
May 1 '07 #11
On May 1, 6:28 am, CBFalconer <cbfalco...@yahoo.comwrote:
Charlie wrote:
I have a file, data.c, where I define all of my global variables.
I then have a header file, data.h, which I include in every file
in which I reference all of the variables defined in data.c.
For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:
enum myenum
{
Mon,
Tues,
Wed
} enum_var;
Now when I try to reference enum_var in data.h, the compiler
gives me issues. I have tried multiple ways of referencing it
and defining it, but nothing is working. I guess I just don't
understand something about enum types and/or extern.

Just like any other type. Define the type in the header file:

enum myenum {Mon, Tues, Wed}; /* note no object */

along with an extrn reference for an object:

extrn enum myenum enum_var;

That handles the include file, which you include in each .c file
that references enum_var. Now each place where it is included knows
what it is, and that it is extrn.

In just ONE of those files you have the object declaration (which
overides the extrn):

enum myenum enum_var;

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account fromhttp://www.teranews.com- Hide quoted text -

- Show quoted text -
Instead, the following would be more appropriate way of solving the
problem,

data.c

#define OWNER_OF_GLOBAL_DATA
#include "data.h"

data.h

#ifdef OWNER_OF_GLOBAL_DATA
#define EXTERN
#else
#define EXTERN exterm
#endif
....
EXTERN enum myenum enum_var;
....

Make sure not to define OWNER_OF_GLOBAL_DATA in all other files where
you want the variables to be used as extern.

;-)
Hope it helps!!!

May 3 '07 #12
On 3 May 2007 01:34:41 -0700, sa*****@yahoo.co.in wrote:
>On May 1, 6:28 am, CBFalconer <cbfalco...@yahoo.comwrote:
>Charlie wrote:
I have a file, data.c, where I define all of my global variables.
I then have a header file, data.h, which I include in every file
in which I reference all of the variables defined in data.c.
For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:
enum myenum
{
Mon,
Tues,
Wed
} enum_var;
Now when I try to reference enum_var in data.h, the compiler
gives me issues. I have tried multiple ways of referencing it
and defining it, but nothing is working. I guess I just don't
understand something about enum types and/or extern.

Just like any other type. Define the type in the header file:

enum myenum {Mon, Tues, Wed}; /* note no object */

along with an extrn reference for an object:

extrn enum myenum enum_var;

That handles the include file, which you include in each .c file
that references enum_var. Now each place where it is included knows
what it is, and that it is extrn.

In just ONE of those files you have the object declaration (which
overides the extrn):

enum myenum enum_var;

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account fromhttp://www.teranews.com- Hide quoted text -

- Show quoted text -

Instead, the following would be more appropriate way of solving the
problem,

data.c

#define OWNER_OF_GLOBAL_DATA
#include "data.h"

data.h

#ifdef OWNER_OF_GLOBAL_DATA
#define EXTERN
#else
#define EXTERN exterm
#endif
And hopefully the compiler error that results from using "exterm"
causes the programmer to ponder the use of objects in header files.
Subtle but effective.

--
jay
May 3 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Alexander Grigoriev | last post: by
20 posts views Thread by Glenn Venzke | last post: by
2 posts views Thread by Sandy | last post: by
8 posts views Thread by Craig Klementowski | last post: by
12 posts views Thread by Laurent Deniau | last post: by
10 posts views Thread by Jay Wolfe | last post: by
2 posts views Thread by Charlie | last post: by
10 posts views Thread by Charlie | last post: by
7 posts views Thread by bashill.zhu | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.