lua5.2调用c++dll
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lua5.2调用c++dll相关的知识,希望对你有一定的参考价值。
cpp源码如下
#include "ELuna.h"
/**/
#define DEBUG_ENGINE
#ifdef DEBUG_ENGINE
//#pragma comment(lib,"../Lib/Lib/Debug/Lua52")
#pragma comment(lib,"../Lib/Debug/Lua52")
#else
#pragma comment(lib,"../Lib/Release/Lua52")
#endif
extern "C" static int Add(lua_State*L)
lua_pushnumber(L, 10);
return 1;
extern "C" static int Sub(lua_State*L)
lua_pushnumber(L, -10);
return 1;
static const struct luaL_Reg testlib[] =
"Add", Add ,
"Sub", Sub ,
NULL, NULL
;
extern "C" __declspec(dllexport) int luaopen_TestDll(lua_State* L)
luaL_newlib(L, testlib);
return 1;
编译后运行lua 得到结果
C:\Users\chenxu>lua
Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio
> require("TestDll")
multiple Lua VMs detected
stack traceback:
[C]: in ?
[C]: in function 'require'
stdin:1: in main chunk
[C]: in ?
>
注意 LUA_BUILD_AS_DLL 这个宏。本回答被提问者采纳 参考技术B 编译的时候调用动态链接库lua52.dll 不要用静态链接库
lua能调用dll吗
lua只能通过ffi库调用外部的。比如alien库。Alien is a Foreign Function Interface (FFI) for Lua. An FFI lets you call functions in dynamic libraries (.so, .dylib, .dll, etc.) from Lua code without having to write, compile and link a C binding from the library to Lua. In other words, it lets you write extensions that call out to native code using just Lua. 参考技术A 可以,只要引擎将lib里面的接口暴露给lua就行了
以上是关于lua5.2调用c++dll的主要内容,如果未能解决你的问题,请参考以下文章