Error 1 Error Lnk2005 Already Defined In .obj
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How can I avoid the LNK2005 linker error for variables defined in a header file? up vote 5 down vote favorite 1 I have 3 cpp files that look like this #include "Variables.h" void AppMain() { //Stuff... } They all use the same variables inside them so they have the same headers but I get stuff like this 1>OnTimer.obj : error LNK2005: "int slider" (?slider@@3HA) already defined in AppMain.obj Why is that? c++ header include linker-error share|improve this question edited Apr 7 '11 at 22:55 Shog9♦ 108k28184217 asked Apr 7 '11 at 0:28 Mark Lalor 3,139134890 4 Put "extern" in front of the variable declarations in the .h file. And define them in one of the .cpp files. Consider using classes instead. –Hans Passant Apr 7 '11 at 0:33 add a comment| 6 Answers 6 active oldest votes up vote 16 down vote accepted Keep in mind that a #include is roughly like cutting and pasting the included file inside the source file that includes it (this is a rough analogy, but you get the point). That means if you have: int x; // or "slider" or whatever vars are conflicting in the header file and that header file is included by three source files in a program, then they will all have a global named x defined that will conflict. What you want to do is define the variable as extern so that the .cpp files will all get the declaration, and then in ONE of your .cpp files give the actual definition. in Variables.h: extern int x; in SomeSourceFile.cpp int x; Of course, I'd recommend against globals, but if you must use them this would keep them from conflicting. share|improve this answer edited Jun 9 '14 at 17:59 slater 3511413 answered Apr 7 '11 at 0:32 James Michael Hare 25.2k55168 add a comm
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Already defined Obj? Linking error? up vote http://stackoverflow.com/questions/5574499/how-can-i-avoid-the-lnk2005-linker-error-for-variables-defined-in-a-header-file 3 down vote favorite 1 I can't for the life of me figure out why I am getting these errors, especially since I have include guards. These are my errors (please ignore what I named my computer): 1>main.obj : error LNK2005: "class std::basic_ostream > >& __cdecl operator<<(class std::basic_ostream > &,class >Date &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVDate@@@Z) already >defined in loan.obj 1>main.obj : error LNK2005: "class std::basic_ostream > >& __cdecl http://stackoverflow.com/questions/19148639/already-defined-obj-linking-error operator<<(class std::basic_ostream > &,class >Loan &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVLoan@@@Z) already >defined in loan.obj 1>C:\Users\SweetAssSarah\Documents\Visual Studio >2012\Projects\ConsoleApplication4\Debug\a1.exe : fatal error LNK1169: one or more multiply >defined symbols found And here are my 4 files: Main.cpp: #ifndef _main_cpp #define _main_cpp #include
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine https://msdn.microsoft.com/en-us/library/72zdcz6f.aspx Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 http://www.gamedev.net/topic/512785-already-defined-in-obj---c/ second. C/C++ Building Reference C/C++ Build Errors Linker Tools Errors and Warnings Linker Tools Errors and Warnings Linker Tools Error LNK2005 Linker Tools Error LNK2005 Linker Tools error 1 Error LNK2005 Linker Tools Error LNK1000 Linker Tools Error LNK1103 Linker Tools Error LNK1104 Linker Tools Error LNK1106 Linker Tools Error LNK1107 Linker Tools Error LNK1112 Linker Tools Error LNK1113 Linker Tools Error LNK1120 Linker Tools Error LNK1123 Linker Tools Error LNK1127 Linker Tools Error LNK1136 Linker Tools Error LNK1140 Linker Tools Error LNK1141 error 1 error Linker Tools Error LNK1143 Linker Tools Error LNK1152 Linker Tools Error LNK1158 Linker Tools Error LNK1164 Linker Tools Error LNK1166 Linker Tools Error LNK1168 Linker Tools Error LNK1169 Linker Tools Error LNK1179 Linker Tools Error LNK1181 Linker Tools Error LNK1188 Linker Tools Error LNK1189 Linker Tools Error LNK1196 Linker Tools Error LNK1200 Linker Tools Error LNK1201 Linker Tools Error LNK1211 Linker Tools Error LNK1215 Linker Tools Error LNK1218 Linker Tools Error LNK1221 Linker Tools Error LNK1223 Linker Tools Error LNK1224 Linker Tools Error LNK1237 Linker Tools Error LNK1240 Linker Tools Error LNK1241 Linker Tools Error LNK1245 Linker Tools Error LNK1248 Linker Tools Error LNK1256 Linker Tools Error LNK1264 Linker Tools Error LNK1277 Linker Tools Error LNK1282 Linker Tools Error LNK1287 Linker Tools Error LNK1296 Linker Tools Error LNK1301 Linker Tools Error LNK1302 Linker Tools Error LNK1306 Linker Tools Error LNK1309 Linker Tools Error LNK1312 Linker Tools Error LNK1313 Linker Tools Error LNK1314 Linker Tools Error LNK1332 Linker Tools Error LNK1561 Linker T
topic ForumsMembersHelp Files Developer Journals Gallery Calendar Downloads Resources Store Classifieds Tracker Links Home For Beginners Articles All Articles Post an Article Technical Game Programming General Programming Graphics Programming and Theory DirectX and XNA OpenGL and Vulkan Multiplayer and Network Programming Artificial Intelligence Math and Physics Mobile Development Middleware, Libraries, and Tools Virtual and Augmented Reality Creative Game Design Music and Sound Visual Arts Business Breaking into the Industry Production and Management Interviews Business and Law Event Coverage Forums All Forums Technical Game Programming General Programming Graphics Programming and Theory DirectX and XNA OpenGL & Vulkan Multiplayer and Network Programming Artificial Intelligence Math and Physics Mobile & Console Development Build Systems & Source Control Middleware, Libraries, and Tools Virtual and Augmented Reality Creative Game Design Writing For Games Music and Sound Visual Arts Gallery Business Breaking into the Industry Business & Law Production & Management Community GDNet Lounge Coding Horrors Article Writing Comments, Suggestions and Ideas Your Announcements Hobby Project Classifieds Indie Project Showcase Community Developer Journals GDNet+ Subscriptions Gallery Store Classifieds Jobs Freelancers Hobby Projects Marketplace Newsletter » Home » Forums » The Technical Side » For Beginners Chat Watched Content New Content 0 "Already defined in .obj" - C++ Started by TutenStain, Oct 26 2008 11:53 AM Old topic! Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic. You cannot reply to this topic 4 replies to this topic #1 TutenStain Members -Reputation: 122 Like 0Likes Like Posted 26 October 2008 - 11:53 AM Hi! I'm new to these forums and I need some help. My current configuration looks like this: Game.h <--In this I keep the global variables. I declare them using "extern". In this I also include Main.h Main.cpp <--On the top of this file I have "#define MAIN_CPP" and then I include Main.h and Game.h. Main.h <--In this I have "#ifndef MAIN_H #define MAIN_H...someRandomFunctios #endif". Active.cpp <--In this I include Game.h Init.cpp <--In this I include Game.h The problem is that im getting an error that says: 1>Init.obj : error LNK2005: "int const SCREEN_WIDTH" (?SCREEN_WIDTH@@3HB) already defined in Active.obj 1>Init.obj : error LNK2005: "int const SCREEN_HEIGHT" (?SCREEN_HEIGHT@@3HB) al