編譯靜態鏈接庫
1.下載Lua源碼
下載後解壓到一個目錄下,這裏假設解壓到D:\win32Lua 注意下載的版本,如果是5.2.x,後面代碼中的C API發生了改變
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 不能一起編譯進去。
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
如果是DEBUG mode 將在Debug目錄下看到一個lua.lib文件,Release mode的lib文件在Release文件下
C/C++代碼中調用lua
2)添加對lua項目的引用
a Common Properties -> Framework and References -> Add New References
選擇lua項目
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.luafunction show()
print("helloworld")
end
show()
運行效果:
引用博文:http://blog.csdn.net/berdy/article/details/7925040