当我在交互式Lua会话期间输入require(“C:/Path/To/Dll/mylib.dll”)时出现此错误
error loading module 'C:\...\mylib.dll' from file 'C:\Program Files\Lua\5.3.4\clib\libgcc_s_dw2-1.dll'
%1 is not a valid Win32 application
当我这样做时:
f = package.loadlib("C:/Path/To/Dll/mylib.dll")
f()
Lua崩溃,如果我运行调试器,我可以看到它在这里崩溃:
l_noret luaG_errormsg (lua_State *L) {
if (L->errfunc != 0) {
StkId errfunc = restorestack(L, L->errfunc);
if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);//<<< HERE
setobjs2s(L, L->top, L->top - 1);
setobjs2s(L, L->top - 1, errfunc);
L->top++;
luaD_call(L, L->top - 2, 1, 0);
}
luaD_throw(L, LUA_ERRRUN);
}
错误源自此处:
LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) {
const lua_Number *v = lua_version(L);
if (v != lua_version(NULL))
luaL_error(L, "multiple Lua VMs detected");//<<< HERE
else if (*v != ver)
luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", ver, *v);
/* check conversions number -> integer types */
lua_pushnumber(L, -(lua_Number)0x1234);
if (lua_tointeger(L, -1) != -0x1234 ||
lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234)
luaL_error(L, "bad conversion number->int;"
" must recompile Lua with proper settings");
lua_pop(L, 1);
}
安装二进制文件时,我可能做错了什么。这些值在我的路径中:
C:\Program Files\Lua\5.3.4\
C:\Program Files\Lua\5.3.4\clibs
我也有这些环境变量:
LUA_CPATH: C:\Program Files\Lua\5.3.4\clib\libgcc_s_dw2-1.dll
LUA_DEV: C:\Program Files\Lua\5.3.4\src
LUA_PATH: C:\Program Files\Lua\5.3.4\?.luac
为了编译我的代码,我在我的解决方案中包含了Lua的源代码(我不认为这是如何完成的,所以这可能是另一个问题)。
这是我的头文件:
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int l_cond ( lua_State * L );
int l_bor ( lua_State * L );
int l_band ( lua_State * L );
int l_bxor ( lua_State * L );
int l_bnot ( lua_State * L );
int l_lshift ( lua_State * L );
int l_rshift ( lua_State * L );
#ifdef __cplusplus
}
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <SDKDDKVer.h>
#include <windows.h>
这是我的C ++代码:
#include "mylib.h"
#ifdef __cplusplus
extern "C" {
#endif
static const luaL_Reg lib[] = {
{ "cond", l_cond },
{ "bor", l_bor },
{ "band", l_band },
{ "bxor", l_bxor },
{ "bnot", l_bnot },
{ "lshift", l_lshift },
{ "rshift", l_rshift },
{NULL, NULL}
};
__declspec( dllexport ) int luaopen_mylib ( lua_State *L )
{
luaL_newlib ( L, lib );
}
static int l_cond ( lua_State *L )
{
/*if ( lua_gettop ( L ) < 3 )
{
return luaL_error ( L, "l_cond expecting 3 arguments" );
}*/
lua_toboolean ( L, 1 ) ? lua_pushvalue ( L, 2 ) : lua_pushvalue ( L, 3 );
return 1;
}
static int l_bor ( lua_State *L )
{
/*if ( lua_gettop ( L ) < 2 )
{
return luaL_error ( L, "l_bor expecting 2 arguments" );
}*/
lua_Integer a = lua_tointeger ( L, 1 );
lua_Integer b = lua_tointeger ( L, 2 );
lua_pushinteger ( L, a | b );
return 1;
}
static int l_band ( lua_State *L )
{
/*if ( lua_gettop ( L ) < 2 )
{
return luaL_error ( L, "l_band expecting 2 arguments" );
}*/
lua_Integer a = lua_tointeger ( L, 1 );
lua_Integer b = lua_tointeger ( L, 2 );
lua_pushinteger ( L, a & b );
return 1;
}
static int l_bxor ( lua_State *L )
{
/*if ( lua_gettop ( L ) < 2 )
{
return luaL_error ( L, "l_bxor expecting 2 arguments" );
}*/
lua_Integer a = lua_tointeger ( L, 1 );
lua_Integer b = lua_tointeger ( L, 2 );
lua_pushinteger ( L, a ^ b );
return 1;
}
static int l_bnot ( lua_State *L )
{
/*if ( lua_gettop ( L ) < 1 )
{
return luaL_error ( L, "l_bxor expecting 1 argument" );
}*/
lua_Integer a = lua_tointeger ( L, 1 );
lua_pushinteger ( L, ~a );
return 1;
}
static int l_lshift ( lua_State *L )
{
/*if ( lua_gettop ( L ) < 2 )
{
return luaL_error ( L, "l_lshift expecting 2 arguments" );
}*/
lua_Integer value = lua_tointeger ( L, 1 );
lua_Integer shift = lua_tointeger ( L, 2 );
lua_pushinteger ( L, value << shift );
return 1;
}
static int l_rshift ( lua_State *L )
{
/*if ( lua_gettop ( L ) < 2 )
{
return luaL_error ( L, "l_rshift expecting 2 arguments" );
}*/
lua_Integer value = lua_tointeger ( L, 1 );
lua_Integer shift = lua_tointeger ( L, 2 );
lua_pushinteger ( L, value >> shift );
return 1;
}
#ifdef __cplusplus
}
#endif
除了这两个文件之外,如上所述,我还在我的项目中包含了整个Lua的源代码。这一切都编译,但我在尝试加载时遇到了这个错误。这对我来说都是新鲜事,我知道我的尝试可能不仅仅是一件事。
答案 0 :(得分:1)
您不应该将Lua源代码包含在项目中。 你应该链接Lua DLL。
更新
与Lua DLL链接的最简单方法,只包括这一行(当然修复路径和库名):
#pragma comment(lib, "C:\\path\\to\\Lua5.X.lib")