lua-intf
lua-intf copied to clipboard
How to start with the wrapper?
Hello,
for the last 2 days I've been trying to figure out how I can use custom C++ library from within Lua and stumbled upon this wrapper, which looks like it does what I need it to do.
So I have this basic library with 1 class:
#pragma once
#include <string>
using namespace std;
class myClass
{
private:
int num;
string str;
public:
myClass(int, string);
void setInt(int);
void setString(string);
int getInt() { return num; }
string getString() { return str; }
};
#include "Header.h"
myClass::myClass(int i, string str) : num(i), str(str)
{ }
void myClass::setInt(int i)
{ this->num = i; }
void myClass::setString(string str)
{ this->str = str; }
I obviously use it in C++ like this
#include <iostream>
#include <Header.h>
using namespace std;
int main()
{
myClass mc(3, "hello");
cout << mc.getInt() << endl << mc.getString() << endl;
mc.setInt(5);
mc.setString("world");
cout << mc.getInt() << endl << mc.getString() << endl;
return 0;
}
is there any way (and if so, how?) to use it from within Lua in a similar way? like this:
local cl = myClass(3, "hello");
print(cl.getInt(), cl.getString());
cl.setInt(5);
cl.setString("world");
print(cl.getInt(), cl.getString());
Does the library has to be compiled as a static, or dynamic library? Do I only include the wrapper and Link the class as shown in the readme? How does the Lua find the linked library then? With C++ I have to link it while compiling, providing a path to the lib. How does this work with Lua?
I'm sorry if this is stupid question, but I've used Lua only for game scripting so far, never really used it with binding to any language, therefore I have no idea how to work with the wrapper provided. Any help is highly appreciated!
You need to manually register the class with the LuaBinding(L).beginClass<myClass>("myClass")
,
see the readme for example.
The lib is headers-only by default, you only need to include "LuaIntf.h" it in your cpp, that's it.
yeah, I understood that from the readme, but what do I compile it as? static lib? dynamic lib? how do I link the lib to the Lua after it's been compiled?
Just like I said, it is headers-only.
All you have to do is add the following line in your cpp file:
#include "LuaIntf/LuaIntf.h"
However, you still need to link with normal lua lib, please refer lua.org for more info.
this WRAPPER is header-only, but I can use it to create C++ libraries for Lua, right? how do I compile THEM? do I compile them as static libs? as dynamic libs? how do I then use the compiled library in Lua?
Not sure about how you use the lua, do you plan to embed lua into you app? or do you plan to create lua module?
Either way, there is no separate compile step for LuaIntf, it get compiled into your cpp directly. Remember LuaIntf is a c++ template library, it is using c++11 template to generate the glue code directly, thus no need to compile it separately.
it seems that you can't understand me...
AFTER I include header of this wrapper in my 'myClass' and bind it using LuaBinding(L).beginClass<myClass>("myClass")
how do I compile myClass? do I compile it as static library? dynamic library? how do I use myClass from within Lua? how does Lua know where to look for myClass? I'm NOT asking about how to include your wrapper in my code, I'm asking about what to do AFTER I include it in my code, how do I compile MY code, MY library [myClass] and how do I use it from within Lua?
local cl = myClass(3, "hello");
print(cl.getInt(), cl.getString());
cl.setInt(5);
cl.setString("world");
print(cl.getInt(), cl.getString());
it seems that you can't understand me...
AFTER I include header of this wrapper in my 'myClass' and bind it using
LuaBinding(L).beginClass<myClass>("myClass")
how do I compile myClass? do I compile it as static library? dynamic library? how do I use myClass from within Lua? how does Lua know where to look for myClass? I'm NOT asking about how to include your wrapper in my code, I'm asking about what to do AFTER I include it in my code, how do I compile MY code, MY library [myClass] and how do I use it from within Lua?
local cl = myClass(3, "hello"); print(cl.getInt(), cl.getString()); cl.setInt(5); cl.setString("world"); print(cl.getInt(), cl.getString());
haha, your question is also that i want to ask. but it's too commonsensical. we should just try it by ourselves.