Lua 5.1 Embedding Into C++ Tutorial (Windows Only)
Posted: Sat Apr 03, 2010 4:45 am
Heya guys, I figured since this took me awhile to get set-up, I would share it with everyone else in a Tutorial fashion since I have not contributed much yet to the forum. I am going to show how to embed Lua into your C++ application with Microsoft Visual C++ 2008 ( on Windows ). I was searching around for ways to run your Lua Script in your C++ application on the web, but unfortunatly I ran into a lot of outdated material. This will show you how with Lua 5.1. ( Including the annoying demands of VC++ ). I admit this tutorial is more based for newbies, like myself. but in these 10 (hopefully) easy to follow steps, you can get up and running. Enjoy .
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..
Then we want to save the file as myLuascript.lua onto your desktop. Now we have our very basic script ready.
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.
Alright so here, were basically doing Three specific things. Initializing Lua, Loading it's library files, and then closing it, fairly simple. So go ahead and try to compile this code, and see if you get any errors, if you do be sure to go back and make sure you have not missed any steps. You also may notice that we need to extern "C" the header files, because Lua was written in C.
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.
There we have it, your Lua Script Running, and C++ following right after, I haven't mingled the two together much, but hopefully soon I'll learn to do all the function calls and what not. If your having trouble getting the script to run, make sure the script file is in the right project directory. If you plan on doing a release build, you will need to set-up your project again with the same steps.
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.
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.