http://www.lua.org/download.html
Step 1: Download Lua and the Stand-Alone Lua Interpreter from here http://code.google.com/p/luaforwindows/
This will come with a compiler called SciTE that you can use to edit your scripts, as well as compile and run them. Be sure to get the Lua Documentation as well, it is extremely helpful.
step 2: After you have the Lua Interpreter/editor set-up, let's make a simple script. Open up SciTE and go to file -> New. Now we want to write something simple in the script, so let's write this script..
Code: Select all
print( "My Lua Script" )
step 3: Now it's time to set up our C++ application. Go ahead and open your VC++ and create a new empty project, make sure console is selected( precompiled headers off ). Name your project to your liking. Create a new source file( .cpp ) called luac.
step 4: Now it's important that we get your Lua include/library files into their appropriate VC++ directories. To do this select tools -> options -> projects and solutions -> VC++ Directories. Now we want to show directories for your include files. Select the "new line" box ( which looks like a folder ) then the ... button, which will allow you to search for the directory in which your Lua Include files are, so look for wherever your Lua is installed ( e.g. C:\Program Files\Lua\5.1\include. When that is finished, select "Show directories" again for library files, and repeat this same process for these.
step 5: VC++ still will not be satisfied, because we have not yet linked the lua object files, so let's do this by going to Project -> luac Properties -> configuration properties -> linker -> Input. Alright when were here, in the box at the top that says "Additional Dependencies, we want to add these in, "lua5.1.lib" "lua51.lib". After this is complete, backtrack a little bit back under the "Linker" selection of your properties, and at the bottom go to "command line". Here we will enter "lua5.1.lib" "lua51.lib" just as we had before, apply and ok.
step 6: Alright now we should be ready to write some C++ code and get Lua initialized, call a script, and close Lua. Then run our C++ application all in one go. So let's try out this code.
Code: Select all
#include <iostream>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
//The interpreter
lua_State *L;
int main()
{
//Initialize Lua
L = lua_open();
//Open Lua Libraries
luaL_openlibs( L );
//close Lua
lua_close( L );
return 0;
}
step 7: So if everything has compiled fine up to this point, we can import our script into C++, but before we do that, lets
make a debug build of your project. After that is built we want to go back into the directory where your Lua is installed, and once your in the Lua/5.1/ directory, find the folder called "lib". Copy the .DLL files from this folder, then we want to go to our project folder directory, and the debug build ( Which we just created ), and paste these .DLL files into it. Now there is one more thing, remember that script we created way earlier and saved to the desktop?, let's copy and paste that into the debug folder as well. At this point you should have copied the Lua .DLL files and the Lua Script you created into your project's debug folder.
step 9: So how do we get this Lua Script to run?, it seems we have Lua set-up in C++, so let's get this thing into action.
Code: Select all
#include <iostream>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
//The interpreter
lua_State *L;
int main()
{
//Initialize Lua
L = lua_open();
//Open Lua Libraries
luaL_openlibs( L );
/* RUN YOUR LUA SCRIPT HERE */
luaL_dofile( L, "myluascript.lua" );
printf( "Now Take a look and notice, that this is C++ Running( after your script ends )" );
//close Lua
lua_close( L );
char exit;
std::cin >> exit;
return 0;
}
step 10: Have fun! Hope this helped, and you learned something out of it. P.S. If there is anything missing out of here that I should know of, or was unaware, please let me know! Thank you everyone.