Lua调用C++带参数的方法

来源:互联网 发布:app开发界面设计软件 编辑:程序博客网 时间:2024/05/17 15:39

C++代码:

// LuaAndC.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>  #include <string>  using namespace std;  extern "C"  {      #include "lua.h"      #include "lauxlib.h"      #include "lualib.h"  }  int sayHello(lua_State *L){string name=luaL_checkstring(L,-1);lua_pushnumber(L,name.length());return 1; //result count}int _tmain(int argc, _TCHAR* argv[]){ //1.创建一个state      lua_State *L = luaL_newstate();  luaL_openlibs(L);lua_register(L,"sayHello",sayHello);luaL_dofile(L,"Hello.lua");    lua_close(L);  int i;cin>>i;    return 0 ;  }


 

Lua代码:

print(sayHello("Hunter"))


 

0 0