1

I'm having a linker issue with mingw on codelite. When I put the .hpp and .cpp files in main where I'm unit testing, everything works fine.

Here's my .hpp file:

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:

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?

user3804116
  • 21
  • 1
  • 3
  • 1
    possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – user657267 Jul 04 '14 at 05:28

1 Answers1

-1

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. typically a directory called include. For example:

-I../include/

This compiler flag will include the parent directory's include folder. All header files in it will become visible to the compiler. Typically all source code or cpp files, are bundled together. Meaning all in the same parent folder for each codelite project.

  • One other thing, try moving your templates into header files.

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

edit:added bullets

  • The code in the question compiles just fine, which clearly indicates that the compiler was able to find the include file. – T.C. Jul 04 '14 at 05:57
  • I was correcting his structure. It is not wise to use absolute linking with quotes. The templates should be in the header file. Proper structure is not always required to compile, but it does make things easier later on. – user3803406 Jul 04 '14 at 16:25
  • Well, all the files are in the same project. I tried and that didn't work either. I also stated that the template class is in Properties.hpp. Edit: Putting the template implementation in the .hpp file worked. Is there any way to separate the template declaration from the implementation? I was looking at this post http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file but I thought I did what some of them said there. – user3804116 Jul 04 '14 at 16:27
  • user657267 answered it with a direct link in relation to the template header issue. They need to be in the header file only. I believe an alternative was given later in that link. – user3803406 Jul 04 '14 at 16:32