0

cmds.cpp has this:

namespace TestNamespace
{
    template<typename... Args>
    void tprint(Args const& ... args)
    {
        std::ostringstream message;
        using List= int[];
        (void)List{0, ((void)(message << args), 0) ... };

        std::cout << message.str();
    }
}

cmds.h has this:

namespace TestNamespace
{
    template<typename... Args> void tprint(Args const& ... args);
}

main.cpp has this:

#include "cmds.h"

using namespace TestNamespace;

void func()
{
    tprint("test\n");
}

Compiler happily compiles. Linker, however, says:

main.cpp: undefined reference to `void TestNamespace::tprint<char [6]>(char const (&) [6])'
collect2: error: ld returned 1 exit status

Makefile flags:

CPPFLAGS=-std=c++11 -m32 -Wall -g -I./ -Wno-trigraphs
LDFLAGS=-std=c++11 -m32 -L./ -lm

What is going on?

Pulseczar
  • 150
  • 8
  • 1
    Templates have static linkage, consequently their definitions must go in the header. – 101010 Aug 17 '16 at 18:54
  • Thanks. I'm quite new to using templates. (obviously, right?) Unfortunately, having to move it to the header file throws a huge wrench into my code, which is a lot more complicated than the stripped-down version I posted. – Pulseczar Aug 17 '16 at 19:18

0 Answers0