Lua:C ++模块不能引用彼此,未定义的符号

时间:2013-01-30 23:45:49

标签: 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中看到确切的符号,但在运行时它不会出于某种原因。

1 个答案:

答案 0 :(得分:2)

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

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

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