VS2010编译Lua程序

来源:互联网 发布:金山数据恢复 收费 编辑:程序博客网 时间:2024/05/16 13:45

找了好久的教程,终于找到一个可以用的,

原文地址:http://blog.csdn.net/berdy/article/details/7925040
1、下载Lua源码 http://www.lua.org/download.html   
a 下载后解压到一个目录下,这里假设解压到D:\lua-5.1.5 b 注意下载的版本,如果是5.2.x,后面代码中的C API发生了改变 后面注释了
2、在VS2010中新建一个静态库项目,项目命名为lua 
a 选择新建 Win32 console project 
b 在wizard界面选择 static Library;不选择Precomplied Header 
3、往工程中添加代码 
D:\lua-5.1.5\src 目录下的*.h文件,*.c文件  添加现有项目  Add Existing Item,到项目的Header Files目录下 ,很多教程说lua.c  和luac.c不要加进去,我加了也没事 
4、配置项目的属性,在项目的“配置属性” 界面中操作 
a Configuration Properties -> C/C++ -> General -> Additional Include Directories  
 添加D:\lua-5.1.5\src 
b Configuration Properties -> C/C++ -> Advanced -> compile as  
 这里的选择将影响后面代码中如何指定编译链接方式,后面的测试选择的是Compile as C code 
5、 生产项目 Build 
如果是DEBUG mode 将在Debug目录下看到一个lua.lib文件,Release mode的lib文件在Release文件下 




后面添加一个项目尝试在C/C++代码中调用lua 
1、在解决方案中添加一个 Win32 console project,项目名称命名为testlua,后面wizard界面中的选项无需修改 
2、添加对lua项目的引用 
a Common Properties -> Framework and References -> Add New References  
 选择lua项目 
3、添加对头文件的include directory 
a Configuration Properties -> C/C++ -> General -> Additional Include Directories  
 添加D:\lua-5.1.5\src 
2、在项目的的在testlua.cpp文件中添加下面的代码 
#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 = lua_open();  
//lua5.2 用这个lua_State *L = luaL_newstate(); 
 
luaL_openlibs(L);  
luaL_dofile(L, "test.lua");  
lua_close(L);  


return 0;



  
}


test.lua 这么写:

 

print("hello, world!\nThis is lua.\n")


按F5或Ctrl+F5,将会在控制台中输入了Hello World




值得注意的: 
前面的4->b步骤中如果选择的是Compile as C++ code,则在上面代码中就不需要extern "C"{},直接include就ok。否则会出现链接错误: 
testlua.obj : error LNK2019: unresolved external symbol _lua_close referenced in function _wmain

0 0
原创粉丝点击