global instances and extern help

W

wakun

Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DS.h"

extern int data;
extern DS stru; /* DS is a struct definied in DS.h */
#endif

/* global.c */
#include "DS.h"
#include "global.h"

int data;
DS stru;
#endif

So, I only need to include global.h when and where I need to access
data and stru. Moreover, though the whole session, only one instance
of data and stru is kept.

Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DC.hpp"

extern int data;
extern DC dc(2); /* DC is a class definied in DC.hpp */
#endif

// global.cpp
#include "DC.hpp"
#include "global.hpp"

int data;
DC dc(2);
#endif

Note that dc take an integer as parameter.

I am intending to keep one instance of DC(i.e. dc) in the whole
project. However, I found it fail to compile the following code

// main.hpp
#include "global.hpp"

int main(void)
{
dc.show();
return 0;
}

Any idea?
 
P

Peter Jansson

Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DS.h"

extern int data;
extern DS stru; /* DS is a struct definied in DS.h */
#endif

/* global.c */
#include "DS.h"
#include "global.h"

int data;
DS stru;
#endif

So, I only need to include global.h when and where I need to access
data and stru. Moreover, though the whole session, only one instance
of data and stru is kept.

Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DC.hpp"

extern int data;
extern DC dc(2); /* DC is a class definied in DC.hpp */
#endif

// global.cpp
#include "DC.hpp"
#include "global.hpp"

int data;
DC dc(2);
#endif

Note that dc take an integer as parameter.

I am intending to keep one instance of DC(i.e. dc) in the whole
project. However, I found it fail to compile the following code

// main.hpp
#include "global.hpp"

int main(void)
{
dc.show();
return 0;
}

Any idea?
Hi,
Could you please show us the compiler error?
Regards,
Peter Jansson
 
A

Artie Gold

Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DS.h"

extern int data;
extern DS stru; /* DS is a struct definied in DS.h */
#endif

/* global.c */
#include "DS.h"
#include "global.h"

int data;
DS stru;
#endif

So, I only need to include global.h when and where I need to access
data and stru. Moreover, though the whole session, only one instance
of data and stru is kept.

Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DC.hpp"

extern int data;
extern DC dc(2); /* DC is a class definied in DC.hpp */
extern DC dc; /* Why are you initializing in a declaration? */
#endif

// global.cpp
#include "DC.hpp"
#include "global.hpp"

int data;
DC dc(2);
#endif

Note that dc take an integer as parameter.

I am intending to keep one instance of DC(i.e. dc) in the whole
project. However, I found it fail to compile the following code

// main.hpp
#include "global.hpp"

int main(void)
{
dc.show();
return 0;
}

Any idea?
HTH,
--ag
 
G

Guest

Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DS.h"

extern int data;
extern DS stru; /* DS is a struct definied in DS.h */
#endif

/* global.c */
#include "DS.h"
#include "global.h"

int data;
DS stru;
#endif

What is this last #endif for?
So, I only need to include global.h when and where I need to access
data and stru. Moreover, though the whole session, only one instance
of data and stru is kept.

Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DC.hpp"

extern int data;
extern DC dc(2); /* DC is a class definied in DC.hpp */
#endif

// global.cpp
#include "DC.hpp"
#include "global.hpp"

int data;
DC dc(2);
#endif

Again, what is this last #endif for?

Note that dc take an integer as parameter.

I am intending to keep one instance of DC(i.e. dc) in the whole
project.

Try to learn about Singleton, a Design Pattern.
Cheers
 
N

Niklas Norrthon

Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

Anything following this line is undefined behavior. Identifiers starting
with an underscore followed by an uppercase letter is reserved for use
by the implementation.

[ snip ]
Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

Likewise.
I suggest you use a pattern for your include guards similar to:
H_FILENAME

If you always start with H_ you also avoid the problem with
reserved identifiers starting with an uppercase E. Also avoid
double underscores everywhere.

A simple rule of thumb, which exclude some legal names, but don't
include any reserved names is (except those of existing functions
and macros defined by the standard library):

Don't start any identifer with underscore, uppercase E, str or mem,
and don't use double underscores anywhere in your identifiers.

If you really want to use names starting with str or mem, append
an underscore (i.e. str_ and mem_).

/Niklas Norrthon
 

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

Help with EXT3 Filesystem work 1
'global' data 11
extern variables 8
extern "C" and JNI 9
variable global and macro #ifdef 10
Help with Extern C 7
header files revisited: extern vs. static 14
help with global variable! 7

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top