简体   繁体   English

Codelite MinGW G ++链接器错误

[英]Codelite MinGW G++ Linker Error

I'm having a linker issue with mingw on codelite. 我在Codelite上遇到mingw的链接器问题。 When I put the .hpp and .cpp files in main where I'm unit testing, everything works fine. 当我将.hpp和.cpp文件放在进行单元测试的主目录中时,一切正常。

Here's my .hpp file: 这是我的.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;
};

Here's my .cpp file: 这是我的.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;
}

And here's my 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;
}

And here's the linker output: 这是链接器的输出:

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

Anyone know what's going on? 有人知道发生了什么吗?

This might apply to you: 这可能适用于您:

  • Move header files out of source directory. 将头文件移出源目录。

It works when they are all in the same folder or not, it is best to relocate them. 无论它们是否都在同一文件夹中,它都可以工作,最好将它们重新放置。 You need to tell the compiler where they are at, in relation to your project. 您需要告诉编译器它们相对于项目的位置。

change to #include <Properties.hpp>

Then place header files into a include directory. 然后将头文件放入包含目录。

Then in compiler flags, add include directory for header files. 然后在编译器标志中,为头文件添加include目录。 typically a directory called include. 通常是一个名为include的目录。 For example: 例如:

-I../include/

This compiler flag will include the parent directory's include folder. 该编译器标志将包含父目录的include文件夹。 All header files in it will become visible to the compiler. 其中的所有头文件将对编译器可见。 Typically all source code or cpp files, are bundled together. 通常,所有源代码或cpp文件都捆绑在一起。 Meaning all in the same parent folder for each codelite project. 对于每个Codelite项目,所有含义均位于同一父文件夹中。

  • One other thing, try moving your templates into header files. 另一件事,请尝试将模板移到头文件中。

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

edit:added bullets 编辑:添加项目符号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM