## 編譯靜態鏈接庫
1.下載[Lua](http://www.lua.org/download.html)源碼?

下載后解壓到一個目錄下,這里假設解壓到D:\win32Lua?注意下載的版本,如果是5.2.x,后面代碼中的C API發生了改變
2)在VS2010中新建一個靜態庫項目,項目命名為lua
a 選擇新建 Win32 console project
b 在wizard界面選擇 static Library;不選擇Precomplied Header
3)往工程中添加代碼
a 復制D:\win32Lua\lua-5.2.3\src 目錄下的*.h文件到項目的Header Files目錄下
b 復制D:\win32Lua\lua-5.2.3\src 目錄下的*.c文件到項目的Code?Files目錄下
注:需要注意的是 lua.c? 和luac.c 不能一起編譯進去。
4)配置項目的屬性,在項目的“配置屬性” 界面中操作
a Configuration Properties -> C/C++-> General -> Additional Include Directories?
添加D:\win32Lua\lua-5.2.3\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:\win32Lua\lua-5.2.3\src
## 示例代碼:
~~~
// 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 = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L,"test.lua");
//const char *buf = "print('Hello World')";
//luaL_dostring(L,buf);
lua_close(L);
return 0;
}
~~~
test.lua
~~~
function show()
print("helloworld")
end
show()
~~~
運行效果:

引用博文:http://blog.csdn.net/berdy/article/details/7925040
- 前言
- lua學習筆記之一(C/C++程序員的Lua快速入門[初階話題])
- lua學習筆記之二(C/C++程序員的Lua快速入門[進階話題])
- lua學習筆記之三(C/C++程序員的Lua快速入門[高階話題])
- lua學習筆記之四(Lua中的基本函數庫)
- lua學習筆記之五(Lua中的數學庫)
- Lua中的table函數庫
- Lua中的常用操作系統庫
- LUA string庫
- LUA IO庫
- VS2010編譯Lua程序(lua-5.2.3)
- Lua中調用C函數(lua-5.2.3)
- Lua 常用數據結構
- lua 如何輸出樹狀結構的table?
- Lua中的元表與元方法
- lua 函數回調技巧
- Cocos2d-x使用Luajit實現加密
- Lua中的模塊與module函數
- 我所理解lua 語言中的點、冒號與self
- Lua代碼編寫規范