Page 1 of 2

Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Sat Apr 03, 2010 4:45 am
by GameDevver4Evr
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..

Code: Select all

 print( "My Lua 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.

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;
}

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.

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;
}

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.

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Sat Apr 03, 2010 6:33 am
by LeonBlade
Cool tutorial, I have a Mac though...

EDIT: That wasn't a lot for this post I apologize.
It was explained well and now I know how I'll use Lua in my project in the future :)

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Sat Apr 03, 2010 11:06 am
by eatcomics
Thank you very much, bookmarked :D I may start doing lua in my new game... But I'm not sure yet... Anyways great tut, and its a great contribution... I've been here for a few years and I've hardly contributed a thing ;)

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Sat Apr 03, 2010 11:45 am
by GroundUpEngine
I used to use Lua but forgot stuff I learned heh. Thanks for the Share ;)

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Sat Apr 03, 2010 2:42 pm
by XianForce
I say this should get added into the Programmer's Education Index Thread =D

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Sat Apr 03, 2010 4:17 pm
by Falco Girgis
XianForce wrote:I say this should get added into the Programmer's Education Index Thread =D
added.

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Sun Apr 04, 2010 5:56 pm
by MrDeathNote
Nice post, really educational :)

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Sun Apr 04, 2010 9:11 pm
by avansc

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Mon Apr 05, 2010 5:13 am
by MrDeathNote
avansc wrote:pachow!
http://csl.sublevel3.org/lua/
Haven't seen that article before, thanks avansc, very nice :)

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Mon Apr 19, 2010 8:12 pm
by ichigo_cool
Very good tutorial! :)
Thanks a lot for this, it worked perfectly!

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Fri Jun 04, 2010 4:47 am
by K-Bal
swarnakshi wrote:How do I become a translator or interpreter for a living? Do I need formal certification? How do I go about finding clients to translate for or companies to work for? If you are experienced as a translator or interpreter, I would appreciate you sharing your experiences and the working conditions, such as a typical day. Thank you.
:lol: Can we get a captcha or something?

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Fri Jun 04, 2010 7:00 am
by programmerinprogress
Excellent! Anyone that demystifies how to implement a scripting language into a C++ project is good in my books. Although I only browsed the post, I will read it at detail and I am bookmarking it since I do hope to one day learn a few scripting languages, including lua.

great job.

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Fri Jun 04, 2010 7:30 am
by Arce
Yes, you've done a fine job with this one. Sometimes, just getting "setup" is half the battle. Thanks alot for the article!

If you've got time, please take a look here:
http://elysianshadows.com/phpBB3/viewto ... 986#p59986

I think this would make a fine addition to the E-Newsletter as a small technical article. ;p

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Fri Jun 04, 2010 1:22 pm
by dandymcgee
K-Bal wrote:
swarnakshi wrote:How do I become a translator or interpreter for a living? Do I need formal certification? How do I go about finding clients to translate for or companies to work for? If you are experienced as a translator or interpreter, I would appreciate you sharing your experiences and the working conditions, such as a typical day. Thank you.
:lol: Can we get a captcha or something?
Wtf? This has gotta be bots for fun, because I'm failing to see who could POSSIBLY profit from that "advertisement". :lol:
If only they'd post in the "Posts not to make" section this would be hilariously amusing.

Re: Lua 5.1 Embedding Into C++ Tutorial (Windows Only)

Posted: Fri Jun 04, 2010 1:26 pm
by short
dandymcgee wrote:
K-Bal wrote:
swarnakshi wrote:How do I become a translator or interpreter for a living? Do I need formal certification? How do I go about finding clients to translate for or companies to work for? If you are experienced as a translator or interpreter, I would appreciate you sharing your experiences and the working conditions, such as a typical day. Thank you.
:lol: Can we get a captcha or something?
Wtf? This has gotta be bots for fun, because I'm failing to see who could POSSIBLY profit from that "advertisement". :lol:
If only they'd post in the "Posts not to make" section this would be hilariously amusing.
The only relevance I see is if you follow the link avansc provided there are advertisements about transcribing software. A faaaaaaaaar stretch but other then that I got nothing.