1

I have re-asked this question here...

Spice
  • 187
  • 1
  • 2
  • 11

1 Answers1

1

You need to include the implementation of each of your functions in Math.h. Declare them inline, e.g.

template<typename T>
inline T max(T a, T b)
{
    return a > b ? a : b;
}

The reason is that when compiling your Math.cpp file, the compiler doesn't know which data types you want to instantiate the function for.

So, inline the functions in Math.h and get rid of Math.cpp.

Sid S
  • 5,907
  • 2
  • 16
  • 23
  • I tried that but it still seemed to produce the same error. – Spice Apr 15 '18 at 04:54
  • Try again. This is the way to do it. – Sid S Apr 15 '18 at 04:55
  • I changed the code now and got rid of the templates thing and also included the "Math.h" header in the "Math.cpp" source file like you said. It still throws the linking error however. – Spice Apr 15 '18 at 05:03
  • I didn't say that you should include `Math.h` in `Math.cpp`. I said you should inline the functions in `Math.h` and ditch `Math.cpp`. – Sid S Apr 15 '18 at 05:04
  • sorry my mistake, but, the thing is by declaring the functions inline, it would remove the entire point of trying to link a static library. – Spice Apr 15 '18 at 05:06
  • There is not much point in putting one-liners in a static library. If you're making one-line template functions, even less so - it only complicates things. – Sid S Apr 15 '18 at 05:10
  • Yes, I know. I am completely aware that this is a bit overkill for such a small file. I am just trying to come up with a simple example for stack overflow. My actual intention is to link the glfw3.a (found [here](http://www.glfw.org/download.html)) static library with my project but it throws this same error. I came up with this example as a small test to see if the problem lies with glfw or with something else... – Spice Apr 15 '18 at 05:13