无法在Lua中加载c dll模块
unable to load c dll module in Lua
我尝试使用"require"在Lua中测试加载c++dll模块,下面是c++模块文件
#include <stdio.h>
#include <iostream>
extern "C" {
#include "lua/lualib.h"
#include "lua/lauxlib.h"
#include "lua/lua.h"
__declspec(dllexport) int luaopen_mylib(lua_State* L);
}
using namespace std;
static int libFunc1(lua_State* L)
{
int n = lua_gettop(L);
printf("in myfunc stack, arg number: %dn", n);
if (lua_isstring(L, -1))
{
std::cout << lua_tostring(L, -1) << std::endl;
}
else
{
std::cout << "invalid arg" << std::endl;
}
return 1;
}
static const struct luaL_Reg mylib[] = {{"func1", libFunc1}, {NULL, NULL}};
int luaopen_mylib(lua_State* L)
{
cout << "loading my lib" << endl;
luaL_newlib(L, mylib);
return 1;
}
我在msys:中使用g++将这个cpp文件编译成dll
g++ -c -o mylib.o mylib.cpp
g++ -shared -o mylib.dll mylib.o -Llua -llua5.3.0
直到现在一切都很好,我也得到了mylib.dll文件。但是当我尝试加载模块时,我得到了错误消息:
> require("mylib")
error loading module 'mylib' from file '.mylib.dll':
找不到指定的程序。
stack traceback:
[C]: in ?
[C]: in function 'require'
stdin:1: in main chunk
[C]: in ?
上面的汉字表示:
The specified function could not be found.
我认为"指定的函数"是指"luaopen_mylib",但cpp文件确实有函数:luaopen_mylib,怎么了?
这可能是名称篡改问题。尝试:
extern "C"
{
int luaopen_mylib(lua_State* L)
{
cout << "loading my lib" << endl;
luaL_newlib(L, mylib);
return 1;
}
}
相关文章:
- 如何告诉本机节点模块所需的dll存储在哪里?
- 如何根据 DLL 端的请求卸载 DLL 模块以卸载它?
- 班级大小因模块 (DLL) 而异.如何以及为什么?
- 如何在 lua cpp 模块中调用托管 c++ dll 函数
- 在特定 Windows DLL 模块中分配内存
- C++ ATIOGLXX.pdb 包含查找模块 ATIOGLXX.dll 源代码所需的调试信息
- 哪种方法更好获取静态链接DLL的模块句柄(HMODULE) - 使用GetModuleHandleEx()或LoadLibrary()?
- 清理跨越Windows DLL模块边界的堆分配资源时出现问题
- QtGui4.lib(QtGui4.dll):致命错误LNK1112:模块计算机类型"X86"与目标计算机类型"x64"冲突
- C# 无法加载 DLL(找不到模块结果:0x8007007E)
- 获取传递给子模块(C#.NET dll)中的主模块(VC++ exe)的参数
- DLL 无法加载:0x8007007E找不到模块。不确定导致此依赖项问题的原因
- 如何在 boost 1.57 中安装提升模块(应用程序和 dll)
- .exe vs .dll 作为一个程序的模块
- 模块"MyShellExt.dll"加载失败
- 无法在Lua中加载c dll模块
- 为什么不是所有的DLL模块我可以加载(打开)
- 在LoadLibrary期间调整DLL模块名称
- 通过元编程或其他方式自动注册dll模块中的静态对象工厂
- 64 位 DLL 模块与文件不匹配