代码:mingwg++链接器错误

Codelite MinGW G++ Linker Error

本文关键字:错误 链接 mingwg++ 代码      更新时间:2023-10-16

我在codelite上有一个链接器问题。当我把。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;
}

这是我的main:

#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:Windowssystem32cmd.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.1bing++.exe   -c  "E:/CodeLite/ElysiumEngine/Interfaces/main.cpp" -g -O0 -Wall  -o ./Debug/main.cpp.o -I. -I.
C:MinGW-4.8.1bing++.exe   -c  "E:/CodeLite/ElysiumEngine/Interfaces/Properties.cpp" -g -O0 -Wall  -o ./Debug/Properties.cpp.o -I. -I.
C:MinGW-4.8.1bing++.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

有人知道发生了什么事吗?

这可能适用于您:

  • 将头文件移出源目录

它工作时,他们都在同一个文件夹或不,最好是重新定位他们。你需要告诉编译器它们在哪里,与你的项目有关。

change to #include <Properties.hpp>

然后将头文件放入include目录。

然后在编译器标志中,添加包含头文件的目录。通常是一个名为include的目录。例如:

-I../include/

这个编译器标志将包含父目录的include文件夹。其中的所有头文件将对编译器可见。通常,所有源代码或cpp文件都捆绑在一起。

    还有一件事,试着把你的模板移到头文件中。

参考http://codelite.org/LiteEditor/ProjectSettings。

编辑:补充子弹

相关文章: