0

When I attempt to build my C++ + SDL project in Visual Studio Express 2013, I get the following error:

1>Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Game::run(void)" (?run@Game@@QAEXXZ) referenced in function _SDL_main

In Main I have #include "TestGame.h"

TestGame.h contains

 #include "Game.h"

struct TestGame : Game{ ... }

And Game.h contains

struct Game{
    ...
    void run();
}

void Game::run is defined in Game.cpp which also includes Game.h

And yet I still receive the error. (And 11 others like it)

I have read the entirety of What is an undefined reference/unresolved external symbol error and how do I fix it? and nothing proposed there seems to fix my problem. I thus do not believe my question to be a duplicate of it.

The entirety of the code can be found at https://github.com/xGeovanni/CPP-Game-Libs

I'd be happy to answer any questions necessary to help me with this problem. Thank you.

EDIT: This problem stopped happening after I deleted the VC++ project and added all the source files to a new one.

Community
  • 1
  • 1
user1149589
  • 243
  • 3
  • 11
  • 1
    And `Game.cpp` is also part of your project source? – πάντα ῥεῖ Jul 06 '14 at 13:09
  • 2
    Are you sure `game.cpp` is actually getting compiled and linked into your project? – Billy ONeal Jul 06 '14 at 13:09
  • 1
    When a linked can't resolve a symbol, the first thing you check is whether the translation unit that you expect to contain that symbol is actually being linked. – David Schwartz Jul 06 '14 at 13:09
  • 1
    @BillyONeal But wait, OP stated to read the usual dupe in entirety, such is already stated there. – πάντα ῥεῖ Jul 06 '14 at 13:10
  • @BillyONeal Visual Studio automatically links everything in your project, doesn't it? You have the C++ file include its own header, and then anything else that includes that header also gets the implementation from the .cpp file, yes? – user1149589 Jul 06 '14 at 13:14
  • 1
    @user1149589: Perhaps you accidentally excluded the file from your project. Could you post the contents of your project file? – Christian Hackl Jul 06 '14 at 13:16
  • 2
    `Visual Studio automatically links everything in your project, doesn't it?` If Game.cpp is listed as a source in your project yes. If not no. – drescherjm Jul 06 '14 at 13:16
  • 1
    The project file is here: https://github.com/xGeovanni/CPP-Game-Libs/blob/master/SDL.vcxproj – drescherjm Jul 06 '14 at 13:17
  • @drescherjim Well it's listed under "Source files" in my Solution Explorer. Does that mean it's listed as a source? – user1149589 Jul 06 '14 at 13:21
  • 2
    Looks suspicious that `Game.cpp` doesn't appear in the primary `` :/ ... – πάντα ῥεῖ Jul 06 '14 at 13:21
  • @ChristianHackl https://www.github.com/xGeovanni/CPP-Game-Libs/blob/master/SDL.vcxproj – user1149589 Jul 06 '14 at 13:24
  • 1
    @user1149589: At SO, linking to external code is usually frowned upon. It's better to edit your answer and include the code there. In any case, it really looks a bit strange. Try deleting the project, creating a new one and adding all the files again. – Christian Hackl Jul 06 '14 at 13:29
  • @ChristianHackl Well I didn't want to just slew all this code in to the main question and clutter it up. How do I bring all the code files in to the new project? – user1149589 Jul 06 '14 at 13:35
  • 1
    I would check to make sure the files are not listed as "Excluded from build" in the project. – drescherjm Jul 06 '14 at 13:37
  • 1
    @user1149589: Well, the usual principle of posting a small but complete code sample applies to all types of code, including VC project files... :) Anyway, just use something like Add > Existing Item in the GUI. – Christian Hackl Jul 06 '14 at 13:37
  • @ChristianHackl Adding all the code to a new project file worked. All seems well again. – user1149589 Jul 06 '14 at 13:45
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – PlasmaHH Jul 07 '14 at 10:42

1 Answers1

2
  <ClInclude Include="Game.cpp" />

This is the reason, the Game.cpp will not be compiled, it has the tag that's only appropriate for an #included file. There are only three files in your project file that have the required <ClCompile> tag (Main.cpp, Player.cpp, TestGame.cpp).

Very unclear how this happened, in general avoid editing a .vcxproj file by hand. To fix the problem, right-click the Game.cpp file in the Solution Explorer, Properties, General. Change the "Item Type" property from "C/C++ Header" to "C/C++ Compiler". Repeat for the other .cpp files that have the wrong tag. You can easily tell which, when you right-click them then they have the Compile menu item disabled.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536