473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 22619
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't...
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
2
by: Sandy | last post by:
I have an enum like this file1.cpp enum e {e1, e2}; extern void fun(); void main() { fun(); }
8
by: Craig Klementowski | last post by:
All, I've installed the VS 2005 Beta 1 and was trying to build our current product. I get a compile error when enum value is specified with classname::enumname::enumvalue. Seems the compiler...
12
by: Laurent Deniau | last post by:
If I understand well, an enumeration is only garantee to hold at most an int (6.7.2.2-2). So I would like to know: how to store a long in an enum? enum { p2_31 = 1L << 31 }; // boom how to...
10
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
2
by: Charlie | last post by:
I use a file to define all of my global variables, data.c, and then I create a header file, data.h, where I reference all the variables with the keyword "extern." I include the header in every...
10
by: Charlie | last post by:
I tried to post this before and I apologize if I am repeating myself, but I do not see the post anywhere. But anyway, I have a file, data.c, where I define all of my global variables. I then...
7
by: bashill.zhu | last post by:
bzhu@TY-PC /h/working/tcplex/ch8/testenum $ cat lexer.h namespace Lexer { enum Token_value; extern Token_value string_value; void get_token(); } bzhu@TY-PC /h/working/tcplex/ch8/testenum
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.