cmds.cpp 有这个:
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 有这个:
namespace TestNamespace
{
template<typename... Args> void tprint(Args const& ... args);
}
main.cpp 有这个:
#include "cmds.h"
using namespace TestNamespace;
void func()
{
tprint("test\n");
}
编译器愉快地编译。然而,链接器说:
main.cpp: undefined reference to `void TestNamespace::tprint<char [6]>(char const (&) [6])'
collect2: error: ld returned 1 exit status
生成文件标志:
CPPFLAGS=-std=c++11 -m32 -Wall -g -I./ -Wno-trigraphs
LDFLAGS=-std=c++11 -m32 -L./ -lm
到底是怎么回事?