當我輸入需要:一個互動的Lua會議期間(「C /Path/To/Dll/mylib.dll」)我得到這個錯誤如何編譯一個C++ dll與Lua一起使用? (我收到錯誤加載模塊)
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);
}
我可能做錯事,而安裝二進制文件。這些值都在我的PATH:
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的源代碼,在我的項目全部。它全部編譯,但是在嘗試加載時出現該錯誤。這對我來說都是非常新的,我知道我的嘗試可能不僅僅是一個錯誤。
有人去除出於某種原因的C標記,即使整個代碼用C寫成...... – FinnTheHuman
編輯歷史中清楚地顯示了「某人」。如果你想問一些問題,直接向人發問,而不是一個蹩腳的筆記。我刪除C標籤的原因是因爲你清楚地詢問了C++ ++ dll並聲明它是C++。 C++不是C,'extern「C」'不會讓你的代碼C代碼!也許你在詢問之前首先下定決心使用哪種語言,而不要使用垃圾郵件標籤。 – Olaf
我只接受Stack Overflow引擎建議的標籤。我認爲,該代碼符合C標準。 – FinnTheHuman