The current state of interfacing Lua with C++ code

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

The current state of interfacing Lua with C++ code

Martin C. Martin-2
Hi,

What libraries would someone recommend for binding Lua to parts of a
large, existing C++ code base?  It needs to work in both Windows and
Linux.  I'm looking at LuaPlus (I know, it's a lot more than just a C++
binding), but perhaps that's not the place to start?

The "Lua C API" page at:

http://lua-users.org/wiki/BindingCodeToLua

mentions 11 projects; which ones would people suggest I try first, and
are there any that are abandoned or superseded or should otherwise be
left until later?

Best,
Martin
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Christopher Eykamp
I'm using Lunar, and have found it not to be too bad once I got the
"rhythm" of it down.  I'm retrofitting it into Bitfighter, a
medium-sized game written in C++, and seems to work for Linux, Windows,
and OS X.

Chris Eykamp



Martin C. Martin wrote:

> Hi,
>
> What libraries would someone recommend for binding Lua to parts of a
> large, existing C++ code base?  It needs to work in both Windows and
> Linux.  I'm looking at LuaPlus (I know, it's a lot more than just a
> C++ binding), but perhaps that's not the place to start?
>
> The "Lua C API" page at:
>
> http://lua-users.org/wiki/BindingCodeToLua
>
> mentions 11 projects; which ones would people suggest I try first, and
> are there any that are abandoned or superseded or should otherwise be
> left until later?
>
> Best,
> Martin
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Carsten Fuchs
In reply to this post by Martin C. Martin-2
Hi Martin,

Martin C. Martin schrieb:
> What libraries would someone recommend for binding Lua to parts of a
> large, existing C++ code base?  It needs to work in both Windows and
> Linux.  I'm looking at LuaPlus (I know, it's a lot more than just a C++
> binding), but perhaps that's not the place to start?

When I started binding Lua to my C++ code in the Cafu game engine <http://www.cafu.de>, my goals
were to keep everything as straightforward  and with as few additional layers of code as
possible, and thus, although I looked into them, I never used any of the available C++-binding
libraries, but just the techniques explained in the PiL2 book.

While this route certainly takes longer in order to see the first results and in fact may seem
cumbersome in a sense (yes, you start thinking about putting the resulting code into a library
of your own... which in a sense means re-inventing the wheel), I still found the results
worthwhile: My code is very short, clean, and I fully understand all the details both on the Lua
and on the C++ side.  :-)  (And I thoroughly learned Lua and its C API this way.)

So my recommendation is to start with the PiL2 book, and only turn to the libraries if you
cannot or don't want to take the "hand-made" approach.

Best regards,
Carsten



--
      Cafu - The Game and Graphics Engine for
multiplayer, cross-platform, real-time 3D Action
             Learn more at www.cafu.de

smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

liam mail


2009/12/9 Carsten Fuchs <[hidden email]>
Hi Martin,

Martin C. Martin schrieb:

What libraries would someone recommend for binding Lua to parts of a large, existing C++ code base?  It needs to work in both Windows and Linux.  I'm looking at LuaPlus (I know, it's a lot more than just a C++ binding), but perhaps that's not the place to start?

When I started binding Lua to my C++ code in the Cafu game engine <http://www.cafu.de>, my goals were to keep everything as straightforward  and with as few additional layers of code as possible, and thus, although I looked into them, I never used any of the available C++-binding libraries, but just the techniques explained in the PiL2 book.

While this route certainly takes longer in order to see the first results and in fact may seem cumbersome in a sense (yes, you start thinking about putting the resulting code into a library of your own... which in a sense means re-inventing the wheel), I still found the results worthwhile: My code is very short, clean, and I fully understand all the details both on the Lua and on the C++ side.  :-)  (And I thoroughly learned Lua and its C API this way.)

So my recommendation is to start with the PiL2 book, and only turn to the libraries if you cannot or don't want to take the "hand-made" approach.

Best regards,
Carsten



--
    Cafu - The Game and Graphics Engine for
multiplayer, cross-platform, real-time 3D Action
           Learn more at www.cafu.de

It depends on what you want from the library which one you should try. For example as you want it to be cross platform that rules out a couple of them. Do you want it to use vanilla Lua or are you OK with it making adjustment to the Lua core, remembering that many addons and debuggers for Lua with a changed core may not work. Then there seems to be some with are no longer in development and also do you want to preform the binding yourself or do you want it to be automatic and the library parses your input files?

If you are thinking of going down the reinvent the wheel route the there is a comparision of the different methods in IIRC Game Programming Gems 6, also some details to consider in an article in Lua Programming Gems. Personally I would say do not invest the time in reinventing the wheel and instead use a try and tested library, yet if everyone did this there would not be so many options (I hold my hand up :) ).

Sorry but if I were to recommend a library it would be a biased answer so I will leave it at that.
Liam
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Jose Luis Hidalgo Valiño
Hi All,

   We always like reinventing the wheel... I did it once, and still
working on it since then, my solution was SLB (Simple Lua binder) ,
now it's hosted at code.google.com, I've been using it for a long time
but I never find time to document it all (its main drawback) but you
can have a loot at the tests to get an idea. For example:

                SLB::Class<Animal, SLB::Instance::NoCopy >("Unit_004::Animal")
                        .set("makeSound", &Animal::makeSound)
                ;

                SLB::Class<Duck>("Unit_004::Duck")
                        .constructor<bool>()
                        .set("canFly", &Duck::canFly)
                        .inherits<Animal>()
                ;

                SLB::Class<Dog>("Unit_004::Dog")
                        .constructor<bool>()
                        .set("bites", &Dog::bites)
                        .inherits<Animal>()
                ;

The project is here:
http://code.google.com/p/slb/

and some tests:
http://code.google.com/p/slb/source/browse/#hg/tests/src

Int addition to be able of using C++ code, you can use hybrid classes
(C++ classes with some methods implemented in Lua):

     class HClass : public SLB::Hybrid<HClass>
        {
        public:
                HClass();

                /* int get() { return LCall<int>("get"); } */
                HYBRID_method_0(get,int);

                /* void calc(int a, int b) { return LCall<void, int,
int>("calc"); } */
                HYBRID_method_2(calc,void,int,int);

                HYBRID_const_method_0(update,void);

....
 in the script:

function HClass:calc(a,b)
        result = a+b
end

-- create one instance
v = HClass()

-- another function:
function HClass:get()
        return result
end

v:calc(6,7)

(result variable is stored in a private table per instance, so you can
extend classes attributes)


Cheers,
   JL

On Wed, Dec 9, 2009 at 1:05 PM, liam mail <[hidden email]> wrote:

>
>
> 2009/12/9 Carsten Fuchs <[hidden email]>
>>
>> Hi Martin,
>>
>> Martin C. Martin schrieb:
>>>
>>> What libraries would someone recommend for binding Lua to parts of a
>>> large, existing C++ code base?  It needs to work in both Windows and Linux.
>>>  I'm looking at LuaPlus (I know, it's a lot more than just a C++ binding),
>>> but perhaps that's not the place to start?
>>
>> When I started binding Lua to my C++ code in the Cafu game engine
>> <http://www.cafu.de>, my goals were to keep everything as straightforward
>>  and with as few additional layers of code as possible, and thus, although I
>> looked into them, I never used any of the available C++-binding libraries,
>> but just the techniques explained in the PiL2 book.
>>
>> While this route certainly takes longer in order to see the first results
>> and in fact may seem cumbersome in a sense (yes, you start thinking about
>> putting the resulting code into a library of your own... which in a sense
>> means re-inventing the wheel), I still found the results worthwhile: My code
>> is very short, clean, and I fully understand all the details both on the Lua
>> and on the C++ side.  :-)  (And I thoroughly learned Lua and its C API this
>> way.)
>>
>> So my recommendation is to start with the PiL2 book, and only turn to the
>> libraries if you cannot or don't want to take the "hand-made" approach.
>>
>> Best regards,
>> Carsten
>>
>>
>>
>> --
>>     Cafu - The Game and Graphics Engine for
>> multiplayer, cross-platform, real-time 3D Action
>>            Learn more at www.cafu.de
>
> It depends on what you want from the library which one you should try. For
> example as you want it to be cross platform that rules out a couple of them.
> Do you want it to use vanilla Lua or are you OK with it making adjustment to
> the Lua core, remembering that many addons and debuggers for Lua with a
> changed core may not work. Then there seems to be some with are no longer in
> development and also do you want to preform the binding yourself or do you
> want it to be automatic and the library parses your input files?
>
> If you are thinking of going down the reinvent the wheel route the there is
> a comparision of the different methods in IIRC Game Programming Gems 6, also
> some details to consider in an article in Lua Programming Gems. Personally I
> would say do not invest the time in reinventing the wheel and instead use a
> try and tested library, yet if everyone did this there would not be so many
> options (I hold my hand up :) ).
>
> Sorry but if I were to recommend a library it would be a biased answer so I
> will leave it at that.
> Liam
>



--
  Jose L. Hidalgo Valiño (PpluX)
  ---- http://www.pplux.com ----
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Miles Bader-2
In reply to this post by Martin C. Martin-2
"Martin C. Martin" <[hidden email]> writes:
> What libraries would someone recommend for binding Lua to parts of a
> large, existing C++ code base?  It needs to work in both Windows and
> Linux.  I'm looking at LuaPlus (I know, it's a lot more than just a C++
> binding), but perhaps that's not the place to start?

I use swig, which is very flexible, works with multiple languages, has
fairly easy/mechanical interface definitions[*], and generates Lua
interfaces which usually capture most of the nuances of the C++
interface (e.g., overloading, inheritance).

[*] It looks at C++ declarations directly, so theoretically you can just
feed it your existing .h files and have it generate the appropriate
interfaces; in practice, you often want to give it slightly massaged
versions, but it's pretty easy to do this.

The downside is that the code it generates can be a bit bloated.  So
it's maybe not what you want for a small embedded app with simple
interfaces.

-Miles

--
Cat, n. A soft, indestructible automaton provided by nature to be kicked when
things go wrong in the domestic circle.
Reply | Threaded
Open this post in threaded view
|

RE: The current state of interfacing Lua with C++ code

Elbers, H.P.
In reply to this post by Martin C. Martin-2
I'm using tolua++ (on a small scale). I think it is a fine tool to bind
small and medium sized C++ libraries.

"tolua++ is an extended version of tolua, a tool to integrate C/C++ code
with Lua. "
http://www.codenix.com/~tolua/

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of
> Martin C. Martin
> Sent: woensdag 9 december 2009 4:46
> To: Lua list
> Subject: The current state of interfacing Lua with C++ code
>
> Hi,
>
> What libraries would someone recommend for binding Lua to
> parts of a large, existing C++ code base?  It needs to work
> in both Windows and Linux.  I'm looking at LuaPlus (I know,
> it's a lot more than just a C++ binding), but perhaps that's
> not the place to start?
>
> The "Lua C API" page at:
>
> http://lua-users.org/wiki/BindingCodeToLua
>
> mentions 11 projects; which ones would people suggest I try
> first, and are there any that are abandoned or superseded or
> should otherwise be left until later?
>
> Best,
> Martin
>
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Pavel Shevaev
On Thu, Dec 10, 2009 at 11:19 AM, Elbers, H.P. <[hidden email]> wrote:
> I'm using tolua++ (on a small scale). I think it is a fine tool to bind
> small and medium sized C++ libraries.
>
> "tolua++ is an extended version of tolua, a tool to integrate C/C++ code
> with Lua. "
> http://www.codenix.com/~tolua/

While tolua++ is nice and we are using it as well, there is also
luabind which I personally find the most advanced Lua binding library
using modern C++ techniques. For example it supports smart pointers
out of the box while tolua++ requires some additional hacking.

Here is the documentation -
http://www.rasterbar.com/products/luabind/docs.html

It also makes sense to use the version from the
repository(http://github.com/luabind/luabind) since the latest stable
release 0.8.1 doesn't compile with boost-1.39+. (update: there is
0.9-rc1 which was just released)




--
Best regards, Pavel
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Martin C. Martin-2
In reply to this post by liam mail
liam mail wrote:
>
> It depends on what you want from the library which one you should try.
> For example as you want it to be cross platform that rules out a couple
> of them.

These are all good questions.

For now I'm doing a proof of concept, so it only needs to work on Linux
x64.  But if things pan out, I'll need a (possibly different) library
that handles both Windows x64 and Linux x64.

> Do you want it to use vanilla Lua or are you OK with it making
> adjustment to the Lua core, remembering that many addons and debuggers
> for Lua with a changed core may not work.

I'd like to avoid that if possible.  One of the reasons for considering
Lua is that performance is important, and I'm hoping to use LJ2 when it
works on x64.

> Then there seems to be some
> with are no longer in development and also do you want to preform the
> binding yourself or do you want it to be automatic and the library
> parses your input files?

At the moment, this is for a proof of concept, so whatever's easiest to
get something up an running is probably best.  Longer term we may care
about micromanaging the resulting bindings, performance, etc.  So
basically, I'm new enough to Lua to not know what the issues are for
this one.

> If you are thinking of going down the reinvent the wheel route

I'd rather not; I tried this last time, and it was very time consuming
to get something basic up and running since I was unfamiliar with all
parts of Lua.  Now I have about 3 days experience in Lua & binding,
which isn't much better. :)

> Sorry but if I were to recommend a library it would be a biased answer
> so I will leave it at that.

Thanks!  With the info above, does that help at all?

Best,
Martin
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Colm Sloan
So to recap, I think this is the list for you, Martin:

Lunar
OOLua
Lua++
SWIG
Luabind
LuaPlus

I'm not sure if any of them are the same thing or not. I may be wrong here but I believe that at the moment, OOLua is the fastest. I tried SWIG and Lua++ but didn't like them. Currently, I use Luabind because it's just easy to work with. Give them all a good read-up before choosing one.
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Martin C. Martin-2
Thanks!

I'll try SWIG first, since I'm not set on using Lua, but want to explore
other languages as well.  Once I settle on an extension language I can
start worrying about performance, etc.

Thanks,
Martin

Colm Sloan wrote:

> So to recap, I think this is the list for you, Martin:
>
> Lunar
> OOLua
> Lua++
> SWIG
> Luabind
> LuaPlus
>
> I'm not sure if any of them are the same thing or not. I may be wrong
> here but I believe that at the moment, OOLua is the fastest. I tried
> SWIG and Lua++ but didn't like them. Currently, I use Luabind because
> it's just easy to work with. Give them all a good read-up before
> choosing one.
Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Asko Kauppi
In reply to this post by Martin C. Martin-2

I ended up doing our own binding (not open source, at least for now -  
sorry).

One of the benefits it has is integration of C++ 'new' operation with  
Lua object lifespan control. So doing

        new(L) MyClass( anything );

..calls the 'MyClass' constructor (of any type) but allocates the  
object in the Lua gc'ed memory area.

If there is a publically available C++/Lua binding library using the  
same mechanism, I'd be glad to know (it would be better to use a  
public library for the binding - or maybe I should push the customer  
to open up that part of the code?).

btw, 'MyClass' needs to derive from 'LuaObject', if anyone starts  
wondering how the above could ever work.

I would warn you about SWIG - imho it's way too complex for the job at  
hand (when Lua is concerned).  I've tried to like it twice, and failed  
both times. :/

-asko


Martin C. Martin kirjoitti 9.12.2009 kello 5:45:

> Hi,
>
> What libraries would someone recommend for binding Lua to parts of a  
> large, existing C++ code base?  It needs to work in both Windows and  
> Linux.  I'm looking at LuaPlus (I know, it's a lot more than just a C
> ++ binding), but perhaps that's not the place to start?
>
> The "Lua C API" page at:
>
> http://lua-users.org/wiki/BindingCodeToLua
>
> mentions 11 projects; which ones would people suggest I try first,  
> and are there any that are abandoned or superseded or should  
> otherwise be left until later?
>
> Best,
> Martin

Reply | Threaded
Open this post in threaded view
|

Re: The current state of interfacing Lua with C++ code

Miles Bader-2
Asko Kauppi <[hidden email]> writes:
> I would warn you about SWIG - imho it's way too complex for the job at
> hand (when Lua is concerned).  I've tried to like it twice, and failed
> both times. :/

I disagree.  I think SWIG is, if anything, one of the _simplest_ to use
for basic interfacing (it's really easy to get a basic interface going),
and yet even this "simple" usage, will give you a pretty complete "Lua
view" of all your C++ classes.  Once you want to do more, it's very
flexible and gives you a lot of power to tweak the interface etc.

Of course it depends on what you want to do -- if you only want to
export a simple list of functions with basic datatypes (int/float/...),
maybe one of the simpler binding libraries will do the job.  But if you
want to take an existing C++ library, including classes, methods,
overloaded functions, etc, etc, SWIG makes the effort required pretty
minimal.

[and of course it's pretty nice that you can then make interfaces for
_other_ languages (python, ruby, ...) with only a little extra work.
Not that I really _want_ to, mind you, but I'm sure there are people out
there...]

-Miles

--
Come now, if we were really planning to harm you, would we be waiting here,
 beside the path, in the very darkest part of the forest?