- 注册时间
- 2011-3-6
- 最后登录
- 1970-1-1
该用户从未签到
|
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、往工程中添加代码
a 复制D:\lua-5.1.5\src 目录下的*.h文件到项目的Header Files目录下
b 复制D:\lua-5.1.5\src 目录下的*.c文件到项目的Header Files目录下
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();
luaL_openlibs(L);
const char *buf = "print('Hello World')";
luaL_dostring(L,buf);
lua_close(L);
return 0;
}
按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 |
|