Jump to content
  • Advertisement

Recommended Posts

Im trying to wrap my head around Entity Component Systems and was implementing my own based on others examples. I have a very basic stripped down version working without systems but as ive started to implement the systems im getting a linker error complaining about unresolved externals.

1>InputSystem.obj : error LNK2019: unresolved external symbol "public: class VelocityComponent * __thiscall Entity::getComponent<class VelocityComponent>(void)const " (??$getComponent@VVelocityComponent@@@Entity@@QBEPAVVelocityComponent@@XZ) referenced in function "public: virtual void __thiscall InputSystem::update(float)" (?update@InputSystem@@UAEXM@Z)

As i understand its complaining that in InputSystem::update() it cannot find the implementation of Entity::getComponent<T>(). InputSystem inherits from system which includes Entity.h.

If i move the implementation of getComponent<T>() from Entity.cpp into Entity.h then everything compiles fine. I thought when including a .h the associated .cpp is compiled along with it into an object file then the linker grabs all that to generate the final executable.

Please help me to understand why this is failing.

Edited by Mr_Mauve

Share this post


Link to post
Share on other sites
Advertisement

Templates behave differently than the standard header/cpp file dichotomy.  There are lots of references online; you should be able to find comprehensive info that explains why you need to have the implementation #included.

Share this post


Link to post
Share on other sites
1 hour ago, Mr_Mauve said:

If i move the implementation of getComponent<T>() from Entity.cpp into Entity.h then everything compiles fine. I thought when including a .h the associated .cpp is compiled along with it into an object file then the linker grabs all that to generate the final executable.

Not exactly.  See also How does the compilation/linking process work? and “Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file?”.

Headers declare existence of symbols, not their actual definition.  Some of examples of such declarations are:

  • Global variable `extern int my_global_int`
  • Global function `bool is_equal(int,int,int)`
  • Class methods `void Foobar::frobnicate(int)`.

Source files provide the definitions of symbols:

  • `int my_global_int = 123;`
  • `bool is_equal(int a, int b, int c) { return a == b && a == c; }`
  • `Foobar::frobnicate(int x) { std::cout << "this=" << this << "x=" << x; }`

The source files are compiled into object code.  Object code is assembly instructions or data with associated symbols.  Headers allow others to know of these symbols’ existence, without having to compile those symbols themselves.  It’s the job of the linker to find all of those external symbols and find their definitions, be it in object code you compiled yourself, in a static library provided elsewhere, or even in a dynamic library.

Templates are a different kind of beast.  They are more code–generating code than they are actual code.  Their symbols and definitions aren’t known until it is used.  For example:

// is_equal.hpp:
template<typename T>
bool is_equal(T a, T b, T c);


// is_equal.cpp:
template<typename T>
bool is_equal(T a, T b, T c) {
    return a == b && a == c;
}

// explicitly instantiate code for `int` and `float`
template<> is_equal<int>(int,int,int);
template<> is_equal<float>(float,float,float);


// main.cpp:
#include "is_equal.hpp"
  
int main() {
  is_equal<int>(1,1,1);           // ok
  is_equal<float>(1.f,1.f,1.f);   // ok
  is_equal<double>(1.0,1.0,1.0);  // Unresolved external symbol
}

 

Unless the definition of `is_equal` is known upfront, then it cannot be used, unless the object code for those types are provided elsewhere.

Share this post


Link to post
Share on other sites

Thank you fastcall22 , the second link was just what i needed.

Because the implementation was in a separate .cpp and i didn't explicitly instantiate the template functions with their types then when the linker went to link the symbol for getComponent<VelocityComponent> it couldn't because its definition was never generated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • 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!