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

问题描述 投票:1回答:1

我的机器上安装了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

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

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个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.