Lua:C ++模块不能引用其他的,未定义的符号 [英] Lua: C++ modules can't reference eachother, undefined symbol

查看:196
本文介绍了Lua:C ++模块不能引用其他的,未定义的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created two modules (shared objects) CPU and SaveState as part of an emulator. Both are independently compiled into .so separate files, and loaded at runtime by a Lua script using require(); i.e.:

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

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

Within CPU, there's a method that operates on a 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 )行失败:

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

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

Using nm -D I can see that exact symbol in savestate.so, but at runtime it's not seen for some reason.

推荐答案

我设法通过编写第三个模块解决这个问题,它在其他两个之前加载,只是调用dlopen luaopen_module方法:

I managed to solve this by writing a third module, which gets loaded 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 this is the best solution, but it seems to do the trick. (I'll have to generalize it a bit to not use hardcoded paths and so on...)

这篇关于Lua:C ++模块不能引用其他的,未定义的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆