我在 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/