In the vs project, use c ++ to call lua and lua to call c
In the previous article, we have discussed how to compile lua source code and generate lua. lib (please refer to use vs2010 to compile lua5.1 source code to generate lua. lib), in the new project, we continue to use the previous project to learn how to use c ++ to call lua.
1. Create a project
In the same solution, right-click solution> Add project> testlua, select win32 console program> no other configuration, and select finish.
Ii. configuration items
Right-click testlua project-> general properties-> framework and reference-> Add a new reference and point to the lua project.
Right-click testlua project-> Configure properties-> c/c ++-> General-> Add the lua Source Code address to the included directory.
Iii. Code call
#include "stdafx.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;}
Iv. Complete
Which lua version does C use to call functions in the lua file?
I use lua 5.2 codeblock to compile
Description and source code
Printf ("% d \ n", (int) lua_tonumber (m_pState, 1 ));
0 should be displayed
Lua Stack
Index 1 => add function
Index 2 => 1
Index 3 => 2
The code used to convert the lua function into a number should be 0.
Surface sentence should get 3
2 0 should be completed unless the code is pasted out and the actual compiled code is the same
How to use C to call LUA