【问题标题】:Codelite MinGW G++ Linker ErrorCodelite MinGW G++ 链接器错误
【发布时间】:2014-07-04 05:24:16
【问题描述】:

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

有人知道怎么回事吗?

【问题讨论】:

标签: c++ linker mingw32 g++4.8


【解决方案1】:

这可能适用于您:

  • 将头文件移出源目录。

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

change to #include <Properties.hpp>

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

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

-I../include/

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

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

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

编辑:添加项目符号

【讨论】:

  • 问题中的代码编译得很好,这清楚地表明编译器能够找到包含文件。
  • 我正在纠正他的结构。使用带引号的绝对链接是不明智的。模板应该在头文件中。编译时并不总是需要正确的结构,但它确实使以后的事情变得更容易。
  • 嗯,所有文件都在同一个项目中。我试过 也没有用。我还说模板类在 Properties.hpp 中。编辑:将模板实现放在 .hpp 文件中有效。有没有办法将模板声明与实现分开?我在看这篇帖子stackoverflow.com/questions/495021/…,但我认为我做了他们中的一些人所说的。
  • user657267 回答了与模板标题问题相关的直接链接。它们只需要在头文件中。我相信稍后在该链接中提供了替代方案。
猜你喜欢
  • 2017-07-28
  • 1970-01-01
  • 2014-09-03
  • 2011-01-03
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多