enum variables and extern

C

Charlie

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
 
G

Guest

Charlie said:
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;
 
M

Martin Ambuhl

Charlie said:
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;
}
 
E

Eric Sosman

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.
 
C

Chris Dollin

Charlie said:
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.
 
C

Charlie

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.
 
C

Charlie

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.

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
 
C

Charlie

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.

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
 
K

Keith Thompson

Harald van Dijk said:
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.
 
B

Bart van Ingen Schenau

Charlie said:
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
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top