1

我在 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

有谁知道发生了什么?

4

1 回答 1

-1

这可能适用于您:

  • 将头文件移出源目录。

无论它们是否都在同一个文件夹中,它都有效,最好重新定位它们。您需要告诉编译器它们与您的项目相关的位置。

change to #include <Properties.hpp>

然后将头文件放入包含目录。

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

-I../include/

此编译器标志将包含父目录的包含文件夹。编译器可以看到其中的所有头文件。通常,所有源代码或 cpp 文件都捆绑在一起。意味着每个 codelite 项目都在同一个父文件夹中。

  • 另一件事,尝试将您的模板移动到头文件中。

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

编辑:添加项目符号

于 2014-07-04T05:52:41.130 回答