win下创建win32控制台工程,执行lua脚本

来源:互联网 发布:北京盛世光明 知乎 编辑:程序博客网 时间:2024/06/06 00:34

1、先创建一个一个静态库工程,创建时,不使用预处理头,把下载的lua源码加入到工程中,记得去掉luac.c文件,编译库文件。

出现问题:

1、1>..\src\lua.c(309): error C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\stdlib.h(449) : 参见“getenv”的声明

解决:把lib工程加入这个宏定义即可。

2、创建一个控制台应用程序,然后把上面创建的库引用进来,加入:

#include "stdafx.h"  
#include <stdio.h>  
#include <string.h>  


extern "C"  
{  
#include <lua.h>  
#include <lualib.h>  
#include <lauxlib.h>  
}


int _tmain(int argc, _TCHAR* argv[])  
{  
lua_State *L = luaL_newstate();  
luaL_openlibs(L);  
luaL_dofile(L,"hello.lua");  


//const char *buf = "print('Hello World')";  
//luaL_dostring(L,buf);  


lua_close(L);  
return 0;  
}  

出现问题:

PANIC: unprotected error in call to Lua API (unable to get ModuleFileName)

在网查找原因后,发现是在编译lua时,项目属性中的字符集若为“使用 Unicode 字符集”话就会出现这个错误。

在这里http://lua-users.org/lists/lua-l/2006-06/msg00427.html有专门的解决方法。

还有一种方法就是将字符集改为“使用多字节字符集”,这样编译生成的lua.lib不会出现上述错误

(至少目前我还没遇到)(摘自:http://blog.csdn.net/iduosi/article/details/7884696)


参考:

1、http://blog.csdn.net/rexuefengye/article/details/26069443

2、http://blog.csdn.net/iduosi/article/details/7884696

0 0