0
\$\begingroup\$

I'm making a simple game in c++, using Visual Studio.

I have a bunch of functions for various things on a file called "engine.h" and then in "main.cpp" I have actual game stuff (levels and map)

I've been trying to put these into classes and then separate the classes into files and include the header files (I think this is called definition and implementation or something like that.)

But every time I try to do this, I get a ton of LINKER errors and have no choice to revert to the 1 header file system. The Linker errors are mostly "LNK2005" and "LNK2001"

Does anyone know how to do this properly? Other than organization, are there any other adverse affects of having all my functions on one header file?

Thanks!

EDIT: Here is my error console

Severity Code Description Project File Line Suppression State Error LNK1169 one or more multiply defined symbols found Game_Multi_File C:\Dev\Game_Multi_File\Debug\Game_Multi_File.exe 1
Error LNK2005 "struct SDL_Window * window" (?window@@3PAUSDL_Window@@A) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "struct SDL_Renderer * renderer" (?renderer@@3PAUSDL_Renderer@@A) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playerY" (?playerY@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playerX" (?playerX@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playerStrength" (?playerStrength@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playerResistance" (?playerResistance@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playerIntelligence" (?playerIntelligence@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playerHealth" (?playerHealth@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playerFlee" (?playerFlee@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playerCurrency" (?playerCurrency@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playerChase" (?playerChase@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int playedFlag" (?playedFlag@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int placeHolderConfig" (?placeHolderConfig@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int fullscreen" (?fullscreen@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int continueKey" (?continueKey@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int checkpointY" (?checkpointY@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "int checkpointX" (?checkpointX@@3HA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "float resizeValue" (?resizeValue@@3MA) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "class std::basic_fstream > playerData" (?playerData@@3V?$basic_fstream@DU?$char_traits@D@std@@@std@@A) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1
Error LNK2005 "class std::basic_fstream > config" (?config@@3V?$basic_fstream@DU?$char_traits@D@std@@@std@@A) already defined in build.obj Game_Multi_File C:\Dev\Game_Multi_File\Game_Multi_File\saving.obj 1

\$\endgroup\$
  • 4
    \$\begingroup\$ You'll need to post an example of the exact error message you're getting (including the real file names involved), and then explain how your project is set up. There are a number of things that can go wrong and cause this error. \$\endgroup\$ – user1118321 Dec 10 '17 at 6:51
  • 1
    \$\begingroup\$ Linker errors can be caused by many things. Most of the times it's because you are declaring a function, but you are not defining it anywhere(/or the linker can't find it). It would be easier to help you, if you include some screenshots of the error console, so that more people can understand the problem and help you diagnose it ;) \$\endgroup\$ – user100681 Dec 10 '17 at 7:48
  • \$\begingroup\$ @GabrieleVierti Ok, the thing is I reverted back to my old system when it didn't work. I'll have to redo it, and then go from there. \$\endgroup\$ – Eyesight Technology Dec 10 '17 at 18:07
  • \$\begingroup\$ @GabrieleVierti So I know it's been I while, but I've managed to replicate the error. (On a smaller scale. I haven't implemented EVERYTHING yet, just a few files as a test.) I have edited my original post to include my error console. \$\endgroup\$ – Eyesight Technology Dec 16 '17 at 18:39
0
\$\begingroup\$

As you can clearly see from the console errors, the compiler is telling you that you alrealdy defined things like SDL_window, SDL_renderer and so on. In a tipical program, for a function/struct class etc.. you have what's called a definition, and a declaration. The definition of a function/struct/class tells the compiler "hey, this function/struct/class exists, it returns this, and has these parameters, so compile this code without any error". The declaration of this function/struct/class is basically what the function should actually do with these parameters and it should return a value(except if the return type is void of course). When the compiler does his work, the next step is the linker: the work of the linker is to go find the functions/structs/classes(their definition according to their declaration) and link everything togheter. If the linker cannot find the declaration of a definition it's going to return an "unresolved external symbol" error. In this case the linker is telling you "hey i found multiple definitions for one declaration, which one should i pick?". It also tells you that it found the declaration of the structs/classes into the build.obj file(.obj files are the result of the operation of the Compiler btw). So you need to verify there is just one declaration of those struct/classes in your program

\$\endgroup\$
  • \$\begingroup\$ Ok, thanks for the clarification! But, how do I fix this? In my code I have a header file with a class. This class contains a bunch of functions. In my CPP file I have all of the content of those functions, outside of any class. The Header file is included by the CPP file. Is there a better way of doing this? \$\endgroup\$ – Eyesight Technology Dec 17 '17 at 14:44
  • \$\begingroup\$ The linker finds out that you have the same symbol defined multiple times in different translation units, and complains about it (it is a violation of the One Definition Rule). The reason is most certainly that main.cpp includes another .cpp file, and both these files are individually processed by the compiler to produce two separate object files. Therefore, all the symbols defined in the other .cpp translation unit will be defined also in the main.cpp translation unit. This is one of the reasons why you do not usually #include .cpp files. \$\endgroup\$ – user100681 Dec 17 '17 at 15:13
  • \$\begingroup\$ I did not include any cpp files. I think the problem is i have a number of variables defined in "saving.h" and I have #included "saving.h" multiple times across multiple cpp files. The problem is both cpp files require these variables, so I don't know where to put them. \$\endgroup\$ – Eyesight Technology Dec 17 '17 at 17:37
  • \$\begingroup\$ Ok, I've figured it out. Using the "extern" keyword in the header files and defining them in one CPP file worked. But, there is one more problem. Unlike all my other variables, I have "SDL_Window *window" and "SDL_Renderer *renderer" I've put them in a header file, and made them "externs" However, now I'm getting this error: "unresolved external symbol" I think I MIGHT know what this means, but have no idea how to fix it. \$\endgroup\$ – Eyesight Technology Dec 18 '17 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.