{"id":407,"date":"2007-11-16T20:44:08","date_gmt":"2007-11-17T02:44:08","guid":{"rendered":"http:\/\/blog.codedread.com\/archives\/2007\/11\/16\/bridging-the-gap-cleanly-setting-up-luabridge\/"},"modified":"2007-11-16T20:44:08","modified_gmt":"2007-11-17T02:44:08","slug":"bridging-the-gap-cleanly-setting-up-luabridge","status":"publish","type":"post","link":"https:\/\/www.codedread.com\/blog\/archives\/2007\/11\/16\/bridging-the-gap-cleanly-setting-up-luabridge\/","title":{"rendered":"Bridging The Gap Cleanly: Setting Up LuaBridge"},"content":{"rendered":"<p>Inspired by <a href=\"http:\/\/www.latenightpc.com\/blog\/archives\/2007\/11\/06\/starting-a-simple-example-using-lua-and-sdl-in-c\/\">Rob<\/a>, I decided I'd take a crack at embedding Lua scripting in a couple C++ projects that I'm working on.  Rob has gone the ultimate route of using <a href=\"http:\/\/www.latenightpc.com\/blog\/archives\/2007\/11\/08\/using-swig-to-connect-c-to-lua\/\">SWIG<\/a>, but when I was starting to do some research, I stumbled upon <a href=\"http:\/\/luabridge.sourceforge.net\/\">Luabridge<\/a>, a library that attempts to do all the interface binding using C++ templates.  This sounds like more fun to me - even if I can't later use it for Scheme scripts or whatever in the future.<!--more--><\/p>\n<div class=\"ads\"><object type=\"text\/html\" width=\"468\" height=\"60\" data=\"http:\/\/www.codedread.com\/gads.php\"><\/object><\/div>\n<div id=\"DownloadLua\">\n<h3>Downloading Lua<\/h3>\n<p>The first step is to install <a href=\"http:\/\/www.lua.org\/\">Lua<\/a> itself.  Download the latest (Lua 5.1.2) from <a href=\"http:\/\/www.lua.org\/ftp\/\">here<\/a>.  Then unzip the package (I used c:\\ so it unrolled into c:\\lua-5.1.2).<\/p>\n<\/div>\n<div id=\"BuildLua\">\n<h3>Bulding Lua<\/h3>\n<p>Next it's time to build Lua.  I originally used Visual Studio .NET 2005 for this, so I went to Start > Program > Microsoft Visual Studio 2005 > Visual Studio Tools > Visual Studio 2005 Command Prompt.  This is a command prompt window with all environment variables set up properly to compile and link from the command line.  In this command prompt window, I went to c:\\lua-5.1.2\\ and typed:  etc\\luavs.bat.  This invoked a batch file that built the Lua libraries.  The files were plopped into c:\\lua-5.1.2\\src:  lua51.dll and lua51.lib. <\/p>\n<p>HOWEVER, later in the process, this ended up causing me no end of trouble.  Specifically, when I later tried to link in the Lua DLL, I started getting run-time errors:  \"\"The application has failed to start because MSVCR80.dll was not found.\"  I then did some cursory searching around the internet and found that I was not alone.  There are <a href=\"http:\/\/www.itwriting.com\/blog\/?postid=261\">several<\/a> <a href=\"http:\/\/www.grimes.demon.co.uk\/workshops\/fusWSThirteen.htm\">articles<\/a> <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms235299(VS.80).aspx\">about<\/a> <a href=\"http:\/\/channel9.msdn.com\/ShowPost.aspx?PostID=23261\">this<\/a>.  I tried several solutions regarding the DLL manifest, but nothing seemed to solve my problem.  One day I'll really have to learn about DLL manifests and assemblies, but for now I just wanted to get this to work like it used to with VC7 and VC6.  At one point, I even got so annoyed as to contemplate building KDE and KDevelop on Windows so I could use the same environment on Linux and Windows - but then I got a-hold of myself.<\/p>\n<p>Ultimately, I downloaded <a href=\"http:\/\/www.mingw.org\/\">MinGW<\/a> and built the Lua libraries using the makefile:<\/p>\n<ul>\n<li>Ensure the MinGW executables are in your path.  Either copy all .exe files to an executable path or add to your PATH environment variable in Windows (right-click My Computer > Properties > Advanced > Environment Variables > System Variables)<\/li>\n<li>In a new command prompt, cd to c:\\lua-5.1.2\\<\/li>\n<li>mingw32-make mingw<\/li>\n<li>Confirm the library files were built:  lua51.lib will be sitting in c:\\lua-5.1.2\\src\\ and lua51.dll will be sitting in c:\\lua-5.1.2\\mingw\\<\/li>\n<\/ul>\n<p>These build instructions are also in the INSTALL file if you need further reference.<\/p>\n<div class=\"ads\"><object type=\"text\/html\" width=\"468\" height=\"60\" data=\"http:\/\/www.codedread.com\/gads.php\"><\/object><\/div>\n<\/div>\n<div id=\"DownloadLuabridge\">\n<h3>Downloading Luabridge<\/h3>\n<p>Now that Lua is built, we're going to install Luabridge, which allows us to cleanly talk between C++ and Lua.  Go download the latest (0.2) from <a href=\"http:\/\/sourceforge.net\/project\/showfiles.php?group_id=190772\">here<\/a>.  Then unzip the package (I used c:\\ so it unrolled into c:\\luabridge-0.2\\).<\/p>\n<\/div>\n<div id=\"BuildLuabridge\">\n<h3>Building Luabridge<\/h3>\n<p>We're getting close now.  Next, we're going to build Luabridge.  There are VC7 (.NET 2003) and VC8 (.NET 2005) projects included in the zip as well as the standard Makefile.  I used the VC8 project, of course.<\/p>\n<p>Open up the solution file and update both projects:  Add c:\\lua-5.1.2\\etc\\ and c:\\lua-5.1.2\\src\\ to the include directories.  Add c:\\lua-5.1.2\\src\\ to the library paths.  Add lua51.lib as a library dependency.<\/p>\n<p>Now build the Luabridge project.  You should see a libluabridge.lib (or libluabridged.lib for Debug) file get created in c:\\luabridge-0.2\\lib\\.<\/p>\n<p>To run the test project in the Luabridge solution, you will have to copy over the lua51.dll from c:\\lua-5.1.2\\src\\ to the \"test\" project's executable path.  Now go to a command prompt, change directory to c:\\luabridge-0.2 and type src\\testd.  You should see a \"All tests succeeded\" message.  This proves that we've built Lua and Luabridge successfully.<\/p>\n<\/div>\n<div id=\"AddLuaAndLuabridge\">\n<h3>Adding Lua and Luabridge To Your Game<\/h3>\n<p>Now is the last step - we're going to integrate Lua with an existing C++ game or project using Luabridge.  Since you're now an expert on Visual Studio project configuration I'll just breeze over the steps:<\/p>\n<ul>\n<li>Add the Lua and Luabridge header files into your include paths<\/li>\n<li>Add the library path to the Lua and Luabridge libraries<\/li>\n<li>Add lua51.lib and libluabridge.lib (libluabridged.lib for Debug) to your dependencies<\/li>\n<li>Copy the lua51.dll into your executable path (Luabridge does not require a DLL as it really only uses C++ template metaprogramming to achieve its ends)<\/li>\n<\/ul>\n<p>An interesting note on Luabridge:  In order to pass C++ objects to Lua using Luabridge, you can't just send Foo* as an argument or a return value to a function.  The reason is that the object pointed to by your Foo pointer might be deleted at some point in C++ land - yet Lua-land won't know that and when it tries to access the object, it will cause problems (crashes).  To help circumvent this, Luabridge requires that pointers to C++ objects be wrapped in a smart pointer, specifically a <i>shared_ptr<\/i> templated object.  This ensures that as long as someone still has a reference to this pointer, the object will not be deleted.<\/p>\n<p>You can use the minimal shared_ptr that comes with Luabridge, or you can use a different shared_ptr template that conforms to the Boost shared_ptr template.  I recommend you use Boost's version if you have the guts to download it, because it allows things like default creation of shared_ptr's without assigning values (useful if you want to have a fixed array of objects, for instance).  Here's how I set up my C++ project to use Boost's shared_ptr with Luabridge:<\/p>\n<p><code><\/p>\n<p>&#160;&#160;&#160;&#160;#include &#60;boost\/shared_ptr.hpp><\/p>\n<p>&#160;&#160;&#160;&#160;using boost::shared_ptr;<\/p>\n<p>&#160;&#160;&#160;&#160;#define USE_OTHER_SHARED_PTR<\/p>\n<p>&#160;&#160;&#160;&#160;#include &#60;luabridge.hpp><\/p>\n<p><\/code><\/p>\n<\/div>\n<p>I'll get into how you can actually code with Luabridge in a later blog post - once you've got it all linked in, it's really not that hard.<\/p>\n<div class=\"ads\"><object type=\"text\/html\" width=\"468\" height=\"60\" data=\"http:\/\/www.codedread.com\/gads.php\"><\/object><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Inspired by Rob, I decided I&#8217;d take a crack at embedding Lua scripting in a couple C++ projects that I&#8217;m working on. Rob has gone the ultimate route of using SWIG, but when I was starting to do some research, I stumbled upon Luabridge, a library that attempts to do all the interface binding using [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,25,11],"tags":[],"class_list":["post-407","post","type-post","status-publish","format-standard","hentry","category-c","category-software","category-technology"],"_links":{"self":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/comments?post=407"}],"version-history":[{"count":0,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/407\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/media?parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/categories?post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/tags?post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}