簡體   English   中英

Codelite MinGW G ++鏈接器錯誤

[英]Codelite MinGW G++ Linker Error

我在Codelite上遇到mingw的鏈接器問題。 當我將.hpp和.cpp文件放在進行單元測試的主目錄中時,一切正常。

這是我的.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

有人知道發生了什么嗎?

這可能適用於您:

  • 將頭文件移出源目錄。

無論它們是否都在同一文件夾中,它都可以工作,最好將它們重新放置。 您需要告訴編譯器它們相對於項目的位置。

change to #include <Properties.hpp>

然后將頭文件放入包含目錄。

然后在編譯器標志中,為頭文件添加include目錄。 通常是一個名為include的目錄。 例如:

-I../include/

該編譯器標志將包含父目錄的include文件夾。 其中的所有頭文件將對編譯器可見。 通常,所有源代碼或cpp文件都捆綁在一起。 對於每個Codelite項目,所有含義均位於同一父文件夾中。

  • 另一件事,請嘗試將模板移到頭文件中。

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

編輯:添加項目符號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM