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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

GameDevver4Evr
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Mon Mar 08, 2010 9:34 pm

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

Post 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.
Last edited by GameDevver4Evr on Sat Apr 03, 2010 11:36 am, edited 1 time in total.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

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

Post 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 :)
There's no place like ~/
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

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

Post 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 ;)
Image
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

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

Post by GroundUpEngine »

I used to use Lua but forgot stuff I learned heh. Thanks for the Share ;)
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

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

Post by XianForce »

I say this should get added into the Programmer's Education Index Thread =D
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post by Falco Girgis »

XianForce wrote:I say this should get added into the Programmer's Education Index Thread =D
added.
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

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

Post by MrDeathNote »

Nice post, really educational :)
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post by avansc »

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

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

Post by MrDeathNote »

avansc wrote:pachow!
http://csl.sublevel3.org/lua/
Haven't seen that article before, thanks avansc, very nice :)
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
ichigo_cool
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 1
Joined: Sun Apr 18, 2010 7:47 pm
Favorite Gaming Platforms: Gameboy Advance, PS2, PS3
Programming Language of Choice: C++

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

Post by ichigo_cool »

Very good tutorial! :)
Thanks a lot for this, it worked perfectly!
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

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

Post 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?
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

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

Post 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.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

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

Post 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
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

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

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

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

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Post Reply