Error: undefined reference to `typeinfo for veins::BatteryAccess'

Error: undefined reference to `typeinfo for veins::BatteryAccess'

我在基于 David Eckhoff Veins/Sumo 项目的 Omnet++ 项目中遇到此错误。

错误是(可能)因为那个 omnet++ 中使用的版本是 4.6 而我使用的是 5.0. 尽管如此,我一直试图找到一个逻辑解决方案,但没有成功。

make MODE=debug all 
src/TutorialAppl.cpp
In file included from src/TutorialAppl.cpp:3:
In file included from src/TutorialAppl.h:12:
In file included from C:/Users/konra/OneDrive/Escritorio/SUMO_VEINS_OMNET/veins-5.0/veins-veins-5.0/src\veins/modules/messages/WaveShortMessage_m.h:33:
C:/Users/konra/OneDrive/Escritorio/SUMO_VEINS_OMNET/veins-5.0/veins-veins-5.0/src\veins/base/utils/Coord.h:329:27: warning: 'veins::Coord::info' redeclared inline; 'dllimport' attribute ignored [-Wignored-attributes]
inline std::string Coord::info() const
                          ^
1 warning generated.
Creating executable: out/clang-debug//VeinsTutorial_dbg.exe
out/clang-debug//src/TutorialAppl.o:(.rdata[_ZTIN5veins9BaseLayerE]+0x10): undefined reference to `typeinfo for veins::BatteryAccess'
out/clang-debug//src/TutorialAppl.o:(.rdata[_ZTIN5veins12BaseMobilityE]+0x10): undefined reference to `typeinfo for veins::BatteryAccess'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:101: out/clang-debug//VeinsTutorial_dbg.exe] Error 1
"make MODE=debug all" terminated with exit code 2. Build might be incomplete.

哦,这是一个编译器错误 (clang)。

clang 编译器不会在 Windows 上将 typeinfo 符号标记为可导出。这通常不会引起注意,除非您尝试在依赖于它的项目中使用来自基础项目(静脉)的抽象 class。摘要 classes 也经常用于定义 Veins、INET 和 OMNeT++ 中的接口。

有一个 workaround for this in INET 也可以在您自己的项目中实现(也可以在 vein 中实现),但是它非常丑陋并且需要重新 link时间所以 link 时间大幅增加。

其他更直接的解决方法:

  • 在 Veins 的 linker 命令行中添加 -Wl,--export-all-symbols 以强制导出所有符号。 (纹理足够小,不会导致 Windows 上导出符号数量的 65K 限制出现问题)
  • 在 Windows 上使用 GCC 而不是 clang(你也应该重建 omnet++)
  • 使用Linux/macOS
  • 使用静态 linking