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

This topic is 4525 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
Declare it like this in the header:
extern const char* textureFileNames[];

Define it like this in a cpp file:
const char* textureFileNames[] = { "texture.tga" };

##### Share on other sites
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.

##### Share on other sites
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 ];

What am I missing here?

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by FellaBut 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.

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 12
• 10
• ### Forum Statistics

• Total Topics
632575
• Total Posts
3007153

×