Jump to content
  • Advertisement
Sign in to follow this  

LNK2005 Error

This topic is 3888 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

I'm getting a LNK2005 error in my program. This didn't happen until I moved a function from a class to a global scope, inside a different header file. This header file is included in 2 different places, and if i comment out one place, I don't get the link error. Here is the file i moved the function to, and thanks for the help
#ifndef COLORS_H
#define COLORS_H

const string CNRM = "\x1B[0m";
const string CRED = "\x1B[31m";
const string CGRN = "\x1B[32m";
const string CYEL = "\x1B[33m";
const string CBLU = "\x1B[34m";
const string CMAG = "\x1B[35m";
const string CCYN = "\x1B[36m";
const string CWHT = "\x1B[37m";
const string CNUL = "";

void writeColor(string Color, string& dest)

	string temp;
	temp = Color;
	temp += dest;
	temp += Color;
	dest = temp;


Share this post

Link to post
Share on other sites
ok I fixed it but i don't understand the difference. I created a .cpp for the header and move the function definition to it, and left the prototype in the .h file... And now no errors... Can anyone explain this?

Share this post

Link to post
Share on other sites
Well i'm somewhat of a beginner, so i'm not entirely sure what you're asking, but i created the file for one project, it is only being used in one project, and it is #included in 3 files in this project. if by lib files you mean .lib, i don't have a clue how to make those, so no

Share this post

Link to post
Share on other sites
Quite simply, you have a symbol that was multiply defined.
You cannot define a symbol twice.

The symbol, in your case, was the function writeColor(). You inadvertently defined it twice because you included the same header (containing this definition) in at least 2 files.
When both files were *compiled* (into .obj files) they both got a copy of the code of 'writeColor()' (or, the definition).
When the linker needed to form an executable from your .obj files (or, 'link your object modules together'), it found two definitions for the same symbol. It doesn't know which to use. Hence, the error. You have to tell it, by only defining any given symbol *once*.

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!