HELP need c guru : extern variable declaration in data.h

Status
Not open for further replies.

coolhand21

Junior Member level 2
Joined
May 17, 2010
Messages
24
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,448
Hi I would like to declare a variable in a data.h and be able to use in other modules.c
--------------------------------------------
data.h
#ifndef _DATA_H_
#define _DATA_H_
int foo = 10;
#endif

--------------------------------------------

#include "data.h"
extern int foo;

void main
{
foo = 12;
test();

}
-------------------------------------------

#include "data.h"
extern int foo;

test()
{
foo = 1+ foo;

}
------------------------------------------
my compiler complains multiple defintions

What i'm I doing wrong
thanks cool
 

data.h
#ifndef _DATA_H_
#define _DATA_H_
extern int foo;
#endif

--------------------------------------------

#include "data.h"
int foo = 10;

void main
{
foo = 12;
test();

}
-------------------------------------------

#include "data.h"
test()
{
foo = 1+ foo;

}
 

Hi friend

you don't declare variable in Header file
Instead of
you can do this

--------------------------------------------

int foo = 10;

void main
{
foo = 12;
test();

}
-------------------------------------------
extern int foo;

test()
{
foo = 1+ foo;

}

Best of Luck
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…