1

I'm getting these error messages

2>main.obj : error LNK2019: unresolved external symbol "public: __thiscall CEngine::CEngine(void)" (??0CEngine@@QAE@XZ) referenced in function _WinMain@16

2>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CEngine::SetWindowSize(int,int,char const *,int)" (?SetWindowSize@CEngine@@QAEXHHPBDH@Z) referenced in function _WinMain@16

2>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CEngine::Begin(void)" (?Begin@CEngine@@QAEXXZ) referenced in function _WinMain@16

2>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall CEngine::GetDisplayWidth(void)" (?GetDisplayWidth@CEngine@@QAEHXZ) referenced in function _WinMain@16

2>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall CEngine::GetDisplayHeight(void)" (?GetDisplayHeight@CEngine@@QAEHXZ) referenced in function _WinMain@16

2>C:\Users\ethan\Desktop\C++ Projects\delveenginetest\Debug\delveenginetest.exe : fatal error LNK1120: 5 unresolved externals

This is my solution:

Solution 'delveenginetest' (2 projects)

DelveEngine

Include

delve.h

Engine.h

SetupSDL.h

stdafx.h

Engine.cpp

Main.cpp

SetupSDL.cpp

This is the code for Engine.h

#pragma once
#include "SetupSDL.h"

class CEngine
{
public:
    CEngine(void);
    ~CEngine(void);

    void SetWindowSize(int winW, int winH, const char* GameName, int windowMode);
    void Begin(void);

    int GetDisplayWidth(void);
    int GetDisplayHeight(void);
private:
    int deskW;
    int deskH;

    bool playing;
    CSetupSDL* sdl_setup;
};

Code for Engine.cpp

#include "Include/stdafx.h"
#include "Include/Engine.h"

CEngine::CEngine(void)
{
    playing = true;

    deskW = GetSystemMetrics(SM_CXSCREEN);
    deskH = GetSystemMetrics(SM_CYSCREEN);
}


CEngine::~CEngine(void)
{
}

void CEngine::SetWindowSize(int winW, int winH, const char* GameName, int windowMode)
{
    // set up SDL for use
    sdl_setup = new CSetupSDL(winW, winH, GameName, windowMode);
}

void CEngine::Begin(void)
{
    while (playing && sdl_setup->GetMainEvent()->type != SDL_QUIT)
    {
        sdl_setup->Begin();
        sdl_setup->End();
    }

    playing = false;
}

int CEngine::GetDisplayWidth(void){ return deskW; }
int CEngine::GetDisplayHeight(void){ return deskH; }

The DelveEngine project builds successfully, whereas the delveenginetest project fails.

What's wrong? I've looked everywhere for a reason, can't find one that suits me.

user3485645
  • 21
  • 1
  • 2
  • 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) – πάντα ῥεῖ Apr 01 '14 at 18:02
  • I've seen that, and it doesn't help me in this case – user3485645 Apr 01 '14 at 18:04
  • Not relevant, but you don't need to say `void` for empty parameter lists in c++. – πάντα ῥεῖ Apr 01 '14 at 18:08
  • I am aware of that. Help would be very nice. – user3485645 Apr 01 '14 at 18:10
  • This is Windows code, most likely in Visual Studio. If so try to right click the [engine.cpp] file, check out its properties. It might be incorrectly treated as a header file (all you have to do get that is to choose the wrong type when you create it). – Cheers and hth. - Alf Apr 01 '14 at 18:14
  • 2
    _'Help would be very nice.'_ Sorry, I can't spot what's wrong actually from the stuff you posted. This probably means the problem lies outside the information space you present here (which is inherently limited by your skills what to look after). I'm a bit suspicious about the 2 projects you're mentioning and what their relation actually is. – πάντα ῥεῖ Apr 01 '14 at 18:15
  • The item type is `C/C++ compiler` as is all the other CPP's, so that's not the issue? – user3485645 Apr 01 '14 at 18:16
  • Well then I'm fresh out of ideas. However, no matter what the problem is, it's easy to **fix**: just create a new project, make sure to add the relevant files, go. – Cheers and hth. - Alf Apr 01 '14 at 18:17
  • What is the result of DelveEngine project, exe, dll, or lib? – Dialecticus Apr 01 '14 at 18:55
  • @user3485645 Just to let you know: I won't retract my down or close votes, unless you are showing some effort to clarify your question. – πάντα ῥεῖ Apr 01 '14 at 19:35

1 Answers1

1

Despite the fact you're not providing all the sufficient information for a correct diagnosis of your problems, I'll try to share what I could imagine that might be the reasons for the linker errors:

  1. I suppose the project delveenginetest you mention is meant to set up unit tests for the classes from the DelveEngine project.
  2. Since you have a Main.cpp in your DelveEngine project, I'd guess it's simply build as an executable (successfully).
  3. Your delveenginetest needs to link to the classes provided from the DelveEngine project, but that's actually not possible, since the .exe from DelveEngine can't be used for linking, you'll need a library to import it to another executable (the unit testing framework).

I'd recommend to separate out your classes/source files from DelveEngine project to make up a static or shared library, that can be linked from an application and the test framework simultaneously from within a single VS solution:

Solution 'DelveEngine' (3 projects)  
   DelveEngineLib (project [.lib/.dll])
      Include  
         delve.h   
         Engine.h   
         SetupSDL.h   
      Engine.cpp
      SetupSDL.cpp
   DelveEngine (project [.exe])
      Main.cpp
   delveenginetest (project [.exe])
      Main.cpp (TestFramework main definition)

Since I'm not very versed with it I don't know actually, if VS 2013 supports to setup projects consuming virtual resources (think of links to sources in the actual build environment), but this could be an alternative how to setup application and unit tests without need of an extra library.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190