Rajveer
Rajveer

Reputation: 857

C++ template class explicit instantiation failing with GCC/NDK

I have a template class in a header .hpp file:

Rage.hpp:

//various includes...

template<typename... Args>
class Rage : public PlatformManagerDelegate
{
    bool paused;

    //other variables...

public:
    Rage(Args... args);
    void pushInitialState(std::unique_ptr<State> state);

    //other methods...
};

and it's implementation in a source .cpp file, with explicit instances at the end of the file:

Rage.cpp:

#include "Rage.hpp"
#include <android_native_app_glue.h>
//other includes...

template<typename... Args>
Rage<Args...>::Rage(Args... args) : paused(false)
{
    //some code...
}

template<typename... Args>
void Rage<Args...>::pushInitialState(std::unique_ptr<State> state)
{
    //some more code...
}

//more methods...

template class Rage<>;
template class Rage<struct android_app*>;

Compiling the following in main.cpp

#include "Rage.hpp"
#include <android_native_app_glue.h>

void android_main(struct android_app* appState)
{
    app_dummy();

    Rage<>* clientInstance = new Rage<>();
    clientInstance->beginRun();
}

gives:

undefined reference to 'Rage<>::beginRun()'
undefined reference to 'Rage<>::Rage()'

This code compiles perfectly fine using MSVC from Visual Studio 2013 (obviously changing android_main to the standard main function), so any help on what I seem to be doing wrong to get it compile on GCC/NDK would be appreciated!

Upvotes: 1

Views: 669

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153955

I'm not an Android developer so I can't reproduce the problem. From a basic C++ perspective it looks as if the file with the instantiations isn't included in the build. Well, that's covering the missing constructor instantiation. The member beginRun() is not defined at all but I assume that it was just cut when copying the code and is actually defined in Rage.cpp.

Upvotes: 1

Related Questions