lua 与 c/c++ 交互(1) 初探

来源:互联网 发布:学哪种编程语言好 编辑:程序博客网 时间:2024/05/21 09:54

lua 与C/C++ 交互


lua 与 C/C++ 交互 有两种方式:1.源码方式 2. 动态链接库方式
首先要做的事,是从lua 官网下载 lua源码,网址 : http://www.lua.org/ftp/点击打开链接

1.源码方式: 
源码方式的 好处 就是 可以调试 和 跟踪 lua代码,
1.1 新建工程,并在工程源码目录中新建 lua 文件夹

1.2解压缩lua源码文件,进入到 src文件夹下,将所有源码拷进工程lua文件中

1.3. 在 资源管理器中 新建 一个 筛选器 lua,将所有拷贝进来的源码(除了Makefile,lua.c,luac.c) 放入到 这个 筛选器中(因为lua.c 和 luac.c 里 定义了 入口文件)

1.4  这时 编译工程 会出现 <<预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)>> 这样的错误, 
只需将lua筛选器中 的.c文件 全部选中, 然后 右键 --> 属性 -->c/c++ --->预编译头-->将预编译头的选项 设置为 不使用  预编译头

1.5接着编译,会出现 << This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.>> 错误,
只需 将 报错的 .c文件 右键 --> 属性 -->C/C++ -->预处理器 --> 预处理器定义-->>加入 "_CRT_SECURE_NO_WARNINGS" 即可,

1.6接着编译,OK了

1.7 测试 
// LuaCApi.cpp : 定义控制台应用程序的入口点。#include "stdafx.h"#include <string.h>extern "C"{//lua是用C写的,所以要按C的方式编译   #include "lua\lua.h"   #include "lua\lauxlib.h"   #include "lua\lualib.h"}int _tmain(int argc, _TCHAR* argv[]){   <span style="white-space:pre"></span>lua_State * l = luaL_newstate();//新建luaState   <span style="white-space:pre"></span>luaL_openlibs(l);//为l 打开所有的 lua库   <span style="white-space:pre"></span>char buffer[255];  <span style="white-space:pre"></span>while (fgets(buffer,sizeof(buffer),stdin))   <span style="white-space:pre"></span>{   <span style="white-space:pre"></span>if (luaL_loadbuffer(l,buffer,strlen(buffer),"line") || lua_pcall(l,0,0,0))   <span style="white-space:pre"></span>{   <span style="white-space:pre"></span>printf("%s :",lua_tostring(l,-1)); //有错误,打印错误信息   <span style="white-space:pre"></span>}  <span style="white-space:pre"></span>}<span style="white-space:pre"></span> lua_close(l); //有打开就有关闭,才是好习惯 <span style="white-space:pre"></span>return 0;}


2.动态库的方式

2.1 生成 动态库 dll 和 lib文件, 进入 vs2012开发人员命令工具 --> 进入 从lua官网下的源码 etc上级目录

我的是:cd C:\Users\Administrator.WIN7U-20140914F\Desktop\lua-5.1.5\lua-5.1.5

--> 输入: etc\luavs.bat

在src目录下 会生成 一些文件,从中 拷贝 .dll .lib 到工程 源码 所在目录中.

2.2在源码所在目录,新建<lua>文件夹,在里面拷贝 lua官网源码 src目录下的 

lua.h <定义了lua的基础函数>

lauxlib.h  <lua 辅助库,函数以 lual_开头,lua作者很邪恶啊,呵呵,撸啊撸>

lualib.h <定义了打开库的方式>

luaconf.h <编译上面三个文件需要用到,所以得加上>


2.3 源码

<span style="font-size:18px;">// luaCApiDll.cpp : 定义控制台应用程序的入口点。#include "stdafx.h"#include <string.h>#pragma comment(lib,"lua51")//动态加载库extern "C"{<span style="white-space:pre"></span>#include "lua\lua.h"<span style="white-space:pre"></span>#include "lua\lauxlib.h"<span style="white-space:pre"></span>#include "lua\lualib.h"}int _tmain(int argc, _TCHAR* argv[]){<span style="white-space:pre"></span>lua_State * l = luaL_newstate();<span style="white-space:pre"></span>luaL_openlibs(l);<span style="white-space:pre"></span>char buffer[256];<span style="white-space:pre"></span>while (fgets(buffer,sizeof(buffer),stdin) != NULL)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if (luaL_loadbuffer(l,buffer,strlen(buffer),"line") || lua_pcall(l,0,0,0))<span style="white-space:pre"></span>{<span style="white-space:pre"></span>printf("error :",lua_tostring(l,-1));<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>lua_close(l);<span style="white-space:pre"></span>return 0;}


0 0
原创粉丝点击