error: undefined reference to 'vtable for android::DongleUsbHost'

本文介绍了一种在Android开发过程中常见的编译错误:链接器未能找到特定类的虚函数表,导致编译失败。通过分析错误日志,指出了解决该问题的方法——添加缺失的析构函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

target SharedLib: libkehdevRuntime (out/target/product/generic/obj/SHARED_LIBRARIES/libkehdevRuntime_intermediates/LINKED/libkehdevRuntime.so)

/home/eagle/android_source/android_4.2/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: out/target/product/generic/obj/SHARED_LIBRARIES/libkehdevRuntime_intermediates/Channel/DongleChannel.o: in function android::DongleUsbHost::DongleUsbHost():device/project/usbhost1.6/kdeviceservice/Channel/DongleChannel.cpp:14: error: undefined reference to 'vtable for android::DongleUsbHost'
/home/eagle/android_source/android_4.2/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: out/target/product/generic/obj/SHARED_LIBRARIES/libkehdevRuntime_intermediates/Channel/DongleChannel.o: in function android::DongleUsbHost::DongleUsbHost():device/project/usbhost1.6/kdeviceservice/Channel/DongleChannel.cpp:14: error: undefined reference to 'VTT for android::DongleUsbHost'
collect2: ld returned 1 exit status
make: *** [out/target/product/generic/obj/SHARED_LIBRARIES/libkehdevRuntime_intermediates/LINKED/libkehdevRuntime.so] Error 1


在anroid中编译实现的代码,出现以上错误。


原因很简单,忘了实现DongleUsbHost的析构函数。实现即可。


好纠结。。。

### 关于 C++ 中 “undefined reference to vtable” 错误的解决方案 当遇到 "undefined reference to vtable" 这样的编译错误时,通常意味着存在一些与虚拟表(vtable)有关的问题。这类问题可能由多种原因引起,具体取决于代码结构和项目配置。 #### 虚函数未实现 如果派生类继承自含有纯虚函数的基础类,则必须确保所有非纯虚成员函数都已提供具体的实现[^3]。例如: ```cpp class Father { public: virtual void function() const = 0; }; class Child : public Father { public: Child(unsigned int data) : mData(data) {} // 必须为父类中的抽象方法提供实际定义 void function() const override {}; private: unsigned int mData; }; ``` 上述例子中,在`Child` 类里实现了来自 `Father` 的纯虚函数 `function()` ,从而避免了链接阶段可能出现的相关错误。 #### 构造/析构函数缺失 另一个常见原因是基类或子类里的某些特殊成员函数(如构造器、析构器)未能正确定义。即使这些函数为空也应显式声明并给出主体[^4]: ```cpp // 正确的做法是在头文件内至少给定默认实现 virtual ~BaseClass() = default; // 或者在源文件(.cpp)中这样写: BaseClass::~BaseClass(){} ``` 这样做可以防止由于缺少必要的符号而导致链接失败的情况发生。 #### 文件管理不当 有时也会因为构建系统设置不正确而引发此类问题。比如使用CMake作为构建工具时,需确认所有的`.cpp`文件都被加入到了目标列表之中: ```cmake add_executable(${PROJECT_NAME} ${SOURCE_FILES} # 包含所有.cpp在内的源码路径 ) ``` 通过以上措施能够有效减少甚至消除“undefined reference to vtable”的出现几率。当然,具体情况还需结合实际开发环境来分析处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值