Lua: C ++ modules cannot refer to each other, undefined character

I created two modules (common objects) CPU and SaveState as part of the emulator. Both of them are independently compiled into .so separate files and loaded at runtime using the Lua script using require (); i.e:.

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

There is a method inside the processor that works with 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; } 

It compiles fine, but the require("CPU") fails:

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

Using nm -D , I can see this exact character in savestate.so, but it did not see it at run time for any reason.

+6
source share
1 answer

I managed to solve this by writing a third module that loads before the other two and just calls dlopen () in its luaopen_module method:

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

I'm not sure if this is the best solution, but it seems like a trick. (I will have to generalize it a bit so as not to use hard-wired paths, etc.)

+2
source

All Articles