c++ - Lua:C++ 模块不能互相引用, undefined symbol

标签 c++ lua dynamic-linking dlopen undefined-symbol

我创建了两个模块(共享对象)CPU 和 SaveState 作为模拟器的一部分。两者都独立编译成 .so 单独的文件,并在运行时由 Lua 脚本使用 require() 加载;即:

SaveState = require("SaveState")
CPU = require("CPU")

在 CPU 中,有一个对 SaveState 进行操作的方法:

int CPU::save_state(SaveState *state) {
    state->begin_section(savestate_namespace, savestate_data_size);

    state->write16(this->reg.af);
    state->write16(this->reg.bc);
    state->write16(this->reg.de);
    state->write16(this->reg.hl);
    state->write16(this->reg.sp);
    state->write16(this->reg.pc);
    state->write8 (this->interrupts_enabled);
    state->write8 (this->irq_flags);
    state->write8 (this->ie_flags);
    state->write8 (this->halted);
    state->write8 (this->halt_bug);
    state->write8 (this->extra_cycles);
    state->write64(this->total_cycles);
    state->write64(this->idle_cycles);

    return SaveState::OK;
}

编译正常,但是 require("CPU") 行失败了:

lua5.1: error loading module 'cpu' from file './src/cpu/build/cpu.so':
    ./src/cpu/build/cpu.so: undefined symbol: _ZN9SaveState7write64Ey

使用 nm -D 我可以在 savestate.so 中看到确切的符号,但在运行时由于某种原因看不到它。

最佳答案

我设法通过编写第三个模块来解决这个问题,该模块先于其他两个模块加载,并在其 luaopen_module 方法中调用 dlopen():

void *res = dlopen("src/savestate/build/savestate.so",
    RTLD_NOW | RTLD_GLOBAL);

我不确定这是最好的解决方案,但它似乎可以解决问题。 (我必须概括一下,不要使用硬编码路径等等...)

关于c++ - Lua:C++ 模块不能互相引用, undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616012/

相关文章:

c++ - OpenCV FaceRecognition 问题 - 它几乎总是匹配图像

c++ - 库中的可执行依赖项

matlab - `GLIBCXX_3.4.11' 未找到,从链接到 glibc 的 MATLAB 运行系统调用不同于 matlab bin 路径中的内容

c++ - 如何将整数转换为其等效的 ascii

c++ - 使用键旋转对象

parsing - Lua:检查表是否可以通过 ipairs & ipairs 从 0 开始循环

c++ - 创建 CCScale9Sprite 时使用 Lua 和 Cocos2d-x 的混淆

c - 如何理解 Relocation section '.rela.plt' 的字段

c++ - .hpp 和 .cpp 文件中的常量数组

lua - 逐字符读取文件到数组