我在 Codelite 上遇到了 mingw 的链接器问题。当我将 .hpp 和 .cpp 文件放在我进行单元测试的 main 中时,一切正常。
这是我的 .hpp 文件:
class ITPropertiesBase
{
public:
virtual ~ITPropertiesBase(){}
virtual const char *getName() = 0;
};
template <typename T>
class Properties : public ITPropertiesBase
{
public:
Properties(const char *name, T value);
~Properties();
const char *getName();
private:
const char *m_name;
T m_value;
};
这是我的 .cpp 文件:
template <typename T> Properties<T>::Properties(const char *name, T value) : m_name(name), m_value(value)
{
}
template <typename T> Properties<T>::~Properties()
{
}
template <typename T> const char* Properties<T>::getName()
{
return m_name;
}
这是我的主要内容:
#include <iostream>
#include <vector>
#include <string>
#include "Properties.hpp"
int main(int argc, char **argv)
{
const char *testInput = "test";
std::vector<ITPropertiesBase*> as;
as.push_back(new Properties<int>(testInput, 5));
return 0;
}
这是链接器输出:
C:\Windows\system32\cmd.exe /c "C:/MinGW-4.8.1/bin/mingw32-make.exe -j4 -e -f Makefile"
"----------Building project:[ Interfaces - Debug ]----------"
mingw32-make.exe[1]: Entering directory 'E:/CodeLite/ElysiumEngine/Interfaces'
C:\MinGW-4.8.1\bin\g++.exe -c "E:/CodeLite/ElysiumEngine/Interfaces/main.cpp" -g -O0 -Wall -o ./Debug/main.cpp.o -I. -I.
C:\MinGW-4.8.1\bin\g++.exe -c "E:/CodeLite/ElysiumEngine/Interfaces/Properties.cpp" -g -O0 -Wall -o ./Debug/Properties.cpp.o -I. -I.
C:\MinGW-4.8.1\bin\g++.exe -o ./Debug/Interfaces @"Interfaces.txt" -L.
./Debug/main.cpp.o: In function `main':
E:/CodeLite/ElysiumEngine/Interfaces/main.cpp:11: undefined reference to `Properties<int>::Properties(char const*, int)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[1]: *** [Debug/Interfaces] Error 1
mingw32-make.exe: *** [All] Error 2
Interfaces.mk:79: recipe for target 'Debug/Interfaces' failed
mingw32-make.exe[1]: Leaving directory 'E:/CodeLite/ElysiumEngine/Interfaces'
Makefile:4: recipe for target 'All' failed
2 errors, 0 warnings
有谁知道发生了什么?