Defining a static array into a C or C++ source fil

2020-02-10 03:53发布

问题:

I know this is a question that every programmer should know, but I do not know. Long time no C programming and I've forgotten a lot of things.

My question is:

I have three huge static arrays defined inside a header file. Someone told me that It's much better to declare them as extern in the header file, and define them in a single C or C++ source file.

How can I do that?

Here is my header file:

#ifndef _TEMPLE_OBJECT_H_
#define _TEMPLE_OBJECT_H_


#define NUM_TEMPLE_OBJECT_VERTEX 10818

static const float TEMPLEVertices[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};
static const float TEMPLENormals[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};
static const float TEMPLETexCoords[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};

#endif

If a use a C++ source file, may I need to define a class?

UPDATE:
I think the problem is:
Every source file in which those headers are included (even indirectly) will generate its own definition for those static arrays. There's no guarantee that the compiler/linker will optimize them into a single definition, even in source files where they're unused. In fact, in many cases the compiler cannot optimize them away. This could result in your static data consuming a lot of disk space, and possibly runtime memory as well.

Thank you.

回答1:

static and extern at the same time makes no sense. static at file scope makes the array inaccessible by other files, while extern tells the compiler that your array is defined somewhere else.

You can do what 321008 suggests, except that you don't declare your arrays static which is illegal C and C++. That gives you three global variables that you can use wherever the header file is included.

For example like this:

// .h file:

extern const float TEMPLEVertices[];

// .cpp (or .c) file:

const float TEMPLEVertices[] = { 1.0, 2.0, 5.6 /* or whatever*/ };

Or you can do what fortran suggests, but that will give you file-scope access only, not global variables.

You do not in any way have to define a class if you use a C++ source file. Unlike Java, C++ does not force you into an object oriented design (whether or not that is good can probably be discussed, but anyway).

EDIT: As for your question update, that is because you define them as static. If you only want global variables, you should not do that, but instead keep one single definition (const float) and reference it with extern, as per my example above.



回答2:

I once saw in the Quake2 source code a funny "trick", that in fact was just an unusual way of using an include:

just do:

static const float TEMPLEVertices[NUM_TEMPLE_OBJECT_VERTEX * 3] = {
#include "TEMPLEVertices.txt"
};

etc.

And keep just the data in the included files.

You can still declare them as extern in the compilation unit, but this keeps things a little bit tidier.



回答3:

Your header file becomes:

#ifndef _TEMPLE_OBJECT_H_
#define _TEMPLE_OBJECT_H_


#define NUM_TEMPLE_OBJECT_VERTEX 10818

extern const float TEMPLEVertices[NUM_TEMPLE_OBJECT_VERTEX * 3];
extern const float TEMPLENormals[NUM_TEMPLE_OBJECT_VERTEX * 3];
extern const float TEMPLETexCoords[NUM_TEMPLE_OBJECT_VERTEX * 3];

#endif

While your source file becomes:

// include header
const float TEMPLEVertices[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};
const float TEMPLENormals[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};
const float TEMPLETexCoords[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...};

// rest of the source

Update: If you are specifying the arrays explicitly, you do not have to mention the sizes. That is you can do:

const float TEMPLEVertices[] = {...};

or

const float TEMPLEVertices[NUM_TEMPLE_OBJECT_VERTEX * 3];


回答4:

It's very simple, actually; I'll demonstrate with an easier example, a simple primitive constant.

In your project, you have two files pi.h and pi.cpp.

The contents of pi.h looks like this:

#ifndef _PI_H
#define _PI_H

extern const double PI;

#endif

The contants of pi.cpp looks like this:

#include "pi.h"

const double PI = 3.1415926535;

If you want to use this constant, you just include pi.h where neccessary. The value will always be read from the same location.

The same can be done with pretty much anything - arrays, objects, arrays of objects, STL containers etc. Just be sure to not overuse this technique - especially when the objects declared extern are not const, you might create some really hard to track side-effects. But for constant data, this is you do it.



回答5:


I usually use a simple trick.

a) In every C/CPP file I define filename_C
b) In every H/HPP file I define filename_H

than...

This is going to be your include file

#ifndef _TEMPLE_OBJECT_H_
#define _TEMPLE_OBJECT_H_

#define NUM_TEMPLE_OBJECT_VERTEX 10818

#ifdef _TEMPLE_OBJECT_C
const float TEMPLEVertices[NUM_TEMPLE_OBJECT_VERTEX * 3] = {...} ; /* Put here your const values */
#else
extern const float TEMPLEVerticies[] ;
#endif

#endif

If I'm not wrong this shoud work (or something very similar to that)... :o)