android - C++ 模板类显式实例化因 GCC/NDK 而失败

标签 android c++ templates gcc android-ndk

我在 header .hpp 文件中有一个模板类:

愤怒.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...
};

它在源 .cpp 文件中实现,在文件末尾有显式实例:

愤怒.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*>;

在 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();
}

给出:

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

此代码使用 Visual Studio 2013 中的 MSVC 编译得非常好(显然将 android_main 更改为标准主函数),因此任何关于我在 GCC/NDK 上编译时似乎做错的帮助都将不胜感激!

最佳答案

我不是 Android 开发人员,因此无法重现该问题。从基本的 C++ 角度来看,带有实例化的文件似乎没有包含在构建中。好吧,这涵盖了缺少的构造函数实例化。成员 beginRun() 根本没有定义,但我假设它只是在复制代码时被剪切,实际上在 Rage.cpp 中定义。

关于android - C++ 模板类显式实例化因 GCC/NDK 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20227327/

相关文章:

c++ - 为什么使用局部结构作为 STL 函数参数的代码不能在 g++ 中编译?

javascript - WebView: Uncaught ReferenceError: Android 未定义

c++ - QProcess:进程仍在运行时被销毁

java - Java 中的安全模板引擎是什么?

c++ - 不能在从 std::vector 继承的类中使用 typedef 迭代器

C++用指针+偏移量读取内存地址

java - 2 个 TabItem 的一个用户列表

Android Assets 文件夹 : it takes always the first file by alphabetical order

android - 如何在 android jelly bean 上读取系统 logcat?

c++ - 强制在 Visual Studio 2015 调试器中加载不匹配的符号