"already defined in *.obj" error although it is "EXTERN"

Started by
5 comments, last by simon10k 17 years, 11 months ago
In my header file I hava a constant declared like this: extern const char* textureFileNames[ NUM_OF_TEXUTRES ] = { "texture.tga" }; This header is included in many .cpp files. Even though I have declared it as extern I still get the error 'LNK2005: "char const * * textureFileNames" (?textureFileNames@@3PAPBDA) already defined in stdafx.obj' whenever I try to use textureFileNames. Do I have to initialize it in a seperate .cpp file even though it is a constant?
People of the Earth unite!
Advertisement
Make sure the line const char* textureFileNames[ NUM_OF_TEXUTRES ] = { "texture.tga" }; is actually written in one of the .cpp files (notice how the extern keyword was left off). Variables with the extern keyword in a header file still need to be defined in a .cpp file.
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.
Declare it like this in the header:
extern const char* textureFileNames[];

Define it like this in a cpp file:
const char* textureFileNames[] = { "texture.tga" };
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
It is strange. I have whole bunch of const declared and initialized in the header file. Many of them I did not even declare as external and it works fine while for some of those which I have declared as extern I get this linking error when declaring and initializing the constants.

When I do it as deathkrush said it works fine but I thought it is recommended to declare and initialize the constants in the header.



People of the Earth unite!
In the following case it is not possible to separate declaration and definition into .h and .cpp file because TEX_NUM must have a value in order to declare textureFileNames[ TEX_NUM ] properly hence it is necessary to do the declaration and definition in the same .h file.

extern const int TEX_NUM;
extern const char* textureFileNames[ TEX_NUM ];

But if I go ahead and declare and define in the same header like so...

extern const int TEX_NUM = 1;
extern const char* textureFileNames[ TEX_NUM ];

... i get the already defined in *.obj linker error.

What am I missing here?
People of the Earth unite!
Why are you declaring an array of const char*s then initializing it with only one const char*?

Do it this way in the header:

#include <vector>#include <string>extern std::vector<std::string> textureFileNames;void loadTextureFileNames();


And then in the cpp, do this:
std::vector<std::string> textureFileNames;void loadTextureFileNames(){   textureFileNames.push_back("texture.tga");   //etc;}


Then call loadTextureFileNames() from inside main. Ther's also a boost library for initialization vectors like raw arrays.
Quote:Original post by Fella
But if I go ahead and declare and define in the same header like so...

extern const int TEX_NUM = 1;
extern const char* textureFileNames[ TEX_NUM ];

... i get the already defined in *.obj linker error.

What am I missing here?


You can declare a name many times, but define it only once.

extern const int TEX_NUM = 1;

is a definition because you provide an initializer.

extern const int TEX_NUM;

is a declaration, the definition exists elsewhere in the program. As a rule of thumb you shouldn't put definitions into header files. The extern is meaningless if you provide an intializer.

some examples...

extern int num1; // declaration
extern int num2 = 10; // definition
int num3; // definition
int num4 = 10; // definition

a definition allocates storages, wheras a declaration says that a name of this type exists somewhere.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003

This topic is closed to new replies.

Advertisement