Omnet+Inet链接错误:undefined reference to typeinfo及邻居列表获取问题
解决OMNeT++中继承AodvRouter后的typeinfo链接错误及邻居列表获取方案
看起来你遇到的是典型的C++动态链接库符号解析错误,这种undefined reference to typeinfo问题在Windows下使用OMNeT++和INET时很常见,主要和DLL的符号导入导出、库链接配置有关。下面分两部分给出解决方案:
一、解决"undefined reference to 'typeinfo for inet::IIpv4RoutingTable'"链接错误
1. 检查项目对INET库的依赖配置
确保你的OMNeT++项目正确链接了INET的库文件:
- 打开项目属性(右键项目 -> Properties)
- 进入
OMNeT++ > Makemake > 选择你的目标(比如src) > Link选项卡 - 在
Additional libraries to link中添加inet4(debug模式下是inet4_dbg),并且确保库的路径指向你的INET4.1.1安装目录的out/gcc-debug或out/gcc-release文件夹
2. 修正INET_API宏的使用
你在MyModule.h中定义了BUILDING_DLL,但这个宏会影响INET头文件中INET_API的行为:
- 如果你的模块是作为INET的扩展模块编译(即编译进INET库),才需要定义
BUILDING_DLL;如果是独立的项目模块,应该移除#define BUILDING_DLL,让INET的头文件自动使用OPP_DLLIMPORT导入符号 - 修改后的
MyModule.h开头:
#ifndef MYMODULE_H_ #define MYMODULE_H_ #include <map> #include "inet/networklayer/ipv4/Ipv4RoutingTable.h" #include <omnetpp.h>
3. 清理并重建项目
编译缓存可能导致符号解析异常,执行以下命令彻底清理后重新编译:
make clean make MODE=debug all
4. 确认编译器版本一致性
确保你的项目使用的编译器和INET编译时使用的编译器一致(OMNeT++5.5.1默认用Clang,不要中途切换到GCC,否则会出现符号不兼容)
二、获取指定设备D的邻居列表的替代方案
其实不需要通过Ipv4RoutingTable来获取邻居,AodvRouter本身就维护了邻居信息,或者可以通过更直接的方式获取:
方案1:直接从AodvRouter获取邻居表
AodvRouter内置了邻居管理模块,你可以直接获取父模块(MyNode继承自AodvRouter)的指针,调用其公开接口获取邻居:
// 在MyModule.cc的initialize中 #include "inet/node/aodv/AodvRouter.h" void MyModule::initialize(){ // 获取父模块的AodvRouter指针 auto aodvRouter = dynamic_cast<AodvRouter*>(getParentModule()); if (aodvRouter) { // 获取邻居表(需要查看AodvRouter的头文件确认公开方法,比如getNeighbors()) // 示例:假设AodvRouter有getNeighborTable()方法 auto neighborTable = aodvRouter->getNeighborTable(); if (neighborTable) { // 遍历邻居 for (auto neighbor : neighborTable->getNeighbors()) { EV_INFO << "Neighbor: " << neighbor->getAddress() << endl; } } } }
方案2:通过InterfaceTable获取链路层邻居
如果需要链路层的邻居(比如ARP表中的设备),可以通过InterfaceTable获取:
// 在MyModule.cc的initialize中 #include "inet/networklayer/common/InterfaceTable.h" #include "inet/linklayer/common/NeighborCache.h" void MyModule::initialize(){ auto interfaceTable = getModuleFromPar<InterfaceTable>(par("interfaceTableModule"), this); for (auto interface : interfaceTable->getInterfaces()) { auto neighborCache = interface->getNeighborCache(); if (neighborCache) { auto neighbors = neighborCache->getNeighbors(); for (auto neighbor : neighbors) { EV_INFO << "Link-layer neighbor: " << neighbor->getMacAddress() << endl; } } } }
方案3:监听AODV Hello消息
AODV通过Hello消息维护邻居,你可以在MyModule中订阅Hello消息,自己维护邻居列表:
- 在NED文件中添加对AODV消息的依赖
- 在initialize中订阅
AodvHelloPacket - 在handleMessage中处理Hello消息,记录邻居信息
内容的提问来源于stack exchange,提问作者Lou_is
相关产品推荐
相关产品推荐

