Lua c lib Windows:找不到指定的程序

Alg*_*thm 2 c windows lua

我的机器上安装了 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;
}
Run Code Online (Sandbox Code Playgroud)

这输出到lualib.dll

我创建了一个脚本,test.lualualib.dll.

require("lualib");
Run Code Online (Sandbox Code Playgroud)

我明白了:

$ 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 ?
Run Code Online (Sandbox Code Playgroud)

然后我尝试

print(package.loadlib("lualib", "luaopen_lualib"));
Run Code Online (Sandbox Code Playgroud)

我得到

$ lua.exe test.lua
nil     The specified procedure could not be found.
        init
Run Code Online (Sandbox Code Playgroud)

我很困惑。我的图书馆在哪里?

Gre*_*een 5

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

__declspec(dllexport) int luaopen_lualib(lua_State *L) {
    luaL_newlib(L, lualib);
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

请参阅lua-users 上的构建模块。

至于更详细的示例,我建议 luasocket: source , header