💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 编译静态链接库 1.下载[Lua](http://www.lua.org/download.html)源码  ![](https://box.kancloud.cn/2016-02-22_56cb2ca316491.jpg) 下载后解压到一个目录下,这里假设解压到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 ![](https://box.kancloud.cn/2016-02-22_56cb2ca3283cf.jpg) 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项目 ![](https://box.kancloud.cn/2016-02-22_56cb2ca339e55.jpg) 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() ~~~ 运行效果: ![](https://box.kancloud.cn/2016-02-22_56cb2ca3498ea.jpg) 引用博文:http://blog.csdn.net/berdy/article/details/7925040