Jump to content
  • Advertisement
Sign in to follow this  
Fella

"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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 ];

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

What am I missing here?

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!