【问题标题】:Lua c lib Windows: The specified procedure could not be foundLua c lib Windows:找不到指定的过程
【发布时间】:2019-08-25 18:20:22
【问题描述】:

我的机器上安装了 Lua 5.3.5 64 位。我正在编译一个 64 位的 dll 来测试 c api 进程。这是我的文件,driver.c

#define LUA_LIB

#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"

static int returnone(lua_State *L) {
    return 1;
}

static const luaL_Reg lualib[] = {
    {"returnone", returnone},
    {NULL, NULL}
};

int luaopen_lualib(lua_State *L) {
    luaL_newlib(L, lualib);
    return 1;
}

这会输出到lualib.dll

我在与lualib.dll 相同的目录中创建了一个脚本test.lua

require("lualib");

我明白了:

$ lua.exe test.lua
C:\Program Files\Lua\lua.exe: error loading module 'lualib' from file '.\lualib.dll':
        The specified procedure could not be found.

stack traceback:
        [C]: in ?
        [C]: in function 'require'
        test.lua:1: in main chunk
        [C]: in ?

那我试试

print(package.loadlib("lualib", "luaopen_lualib"));

我得到了

$ lua.exe test.lua
nil     The specified procedure could not be found.
        init

我被难住了。我的图书馆呢?

【问题讨论】:

  • 你用的是什么编译器?

标签: c windows lua


【解决方案1】:

将 Lua 模块构建到 windows DLL 时,您需要使用 __declspec(dllexport) 例如对于最简单的情况,这应该足够了:

__declspec(dllexport) int luaopen_lualib(lua_State *L) {
    luaL_newlib(L, lualib);
    return 1;
}

请参考 lua-users 上的Building Modules

至于更详细的示例,我建议使用 luasocket:sourceheader

【讨论】:

    猜你喜欢
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    相关资源
    最近更新 更多