包装 c++使用 SWIG 在 Lua 中使用它的类 - 需要简单的例子
在使用 SWIG 和 lua 方面,我是一个绝对的初学者,也是一个平庸的 C++ 开发人员,而且我似乎不明白如何将 C++ 类与 Lua 绑定。
我的最终目标是拥有一个类的实例,将其传递给我的 lua 脚本,该脚本以某种方式操作该对象,然后我在 C++ 中检索该对象并执行我想做的任何其他操作。
所以...我已经下载了 Lua 和 C++,编译了一些示例,我能够在我的 C++ 程序中运行一些 lua 脚本。到目前为止,一切都很好。
现在,我已经下载了 SWIG。我在 Windows 7 上使用 VC2010。据我了解,我应该创建一个接口文件。所以我创建了一个 example.i:(
%module creature
class Creature
{
public:
Creature(void);
Creature(int id);
~Creature(void);
(...) the rest of my class here
刚刚将 %module 生物添加到我的 Creature.h 并将其另存为creature.i)。我使用运行 SWIG
swig -c++ -lua creature.i
并得到了我的 Bio.cxx 文件。
现在,我可能会让自己感到尴尬,但是……我不知道下一步该怎么做。我应该以某种方式构建输出文件吗?我现在该如何使用它?我在这里读过http://www.swig.org/Doc1.3/Lua。在 html 中,我需要将生成的文件 (*.cxx) 与我的代码的其余部分链接起来(因此我只需将该文件包含在我的项目中并添加行 #include "Creature.h" 即可)。但是当我编译时,我遇到了错误,就像
error C2065: 'LUA_GLOBALSINDEX' : undeclared identifier
error C2036: 'const luaL_reg *' : unknown size
我也尝试使用 MinGW 编译它一样,但我遇到了同样的问题。如果有人可以向我展示如何一起使用这些工具的示例或分步教程,那就太好了,因为我很难掌握整个想法。这绝对是由于我对如何使用它的基础知识缺乏了解,所以请对菜鸟温柔一点。
任何帮助将不胜感激。
I'm an absolute beginner when it comes to using both SWIG and lua, and a mediocre C++ developer, and I just don't seem to understand how I can bind C++ classes with Lua.
My end goal is to have an instance of a class, pass it to my lua script which manipulates that object in a certain way, then I retrieve that object in c++ and do whatever else I want to do with it.
So... I've downloaded Lua and C++, compiled a few examples, I am able to run some lua script in my c++ program. So far so good.
Now, I've downloaded SWIG. I'm using VC2010 on Windows 7. From what I understand I'm suppose to create an interface file. So I've created one, example.i:
%module creature
class Creature
{
public:
Creature(void);
Creature(int id);
~Creature(void);
(...) the rest of my class here
(Just added %module creature to my Creature.h and saved it as creature.i). I ran SWIG using
swig -c++ -lua creature.i
and got my creature.cxx file.
Now, I'll probably embarrass myself now but... I don't know what to do next. Am I suppose to build the output file somehow? How can I use it now? I've read here http://www.swig.org/Doc1.3/Lua.html that I need to link the generated file (*.cxx) with the rest of my code (so I do that by simply including the file in my project and adding the line #include "Creature.h"). But when I compile I get errors like
error C2065: 'LUA_GLOBALSINDEX' : undeclared identifier
error C2036: 'const luaL_reg *' : unknown size
I have also tried compiling it using MinGW but I get the same problem. It would be great if someone could show me an example or a step-by-step tutorial of how I can use these tools together, as I have a hard time grasping the whole idea. This is definitely due to my lack of understanding of the basics of how this should be used so please be gentle to a noob.
Any help will be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SWIG 是一种将脚本语言链接到 C 或 C++ 代码的工具。它作为一个预处理步骤:您在 .swig 文件上运行 SWIG 可执行文件,该文件会生成一些 C 或 C++ 代码。然后,您可以将该代码构建到您想要链接到该脚本语言的任何项目中。
SWIG 的 Lua 支持肯定与 Lua 5.2 不兼容。事实上,您会发现与 Lua 5.2 兼容的代码非常少。如果您想实际使用 Lua 代码做一些事情,至少暂时坚持使用 5.1。
SWIG is a tool for linking scripting languages to C or C++ code. It works as a preprocess step: you run the SWIG executable on a .swig file, which produces a bit of C or C++ code. You then build that code into whatever project you want to do the linking to that scripting language for.
SWIG's Lua support is most certainly not compatible with Lua 5.2. Indeed, you will find very little code out there that is compatible with Lua 5.2. If you want to actually do something with Lua code, stick with 5.1 at least for the time being.
我可能是错的,但你不会将 C++ 类的实例“传递”给 Lua,你真正做的是创建与 Lua 的 C++ 绑定,从而实际上在 Lua 脚本本身中实例化 C++ 类。
即假设您有一个名为 Foo 的 C++ 类,其中有一个名为 add(x, y) 的方法。然后,您可以通过执行类似的操作在 Lua 中实例化并使用该类(假设您已使用 Swig 创建包装文件)。
我在 C++ 程序中使用 Swig 和 Lua 时遇到了一些不幸,所以我写了一个教程(这样我就不会忘记我是如何做到的)。您可以在这里查看... http: //glennmccord.wordpress.com/2012/11/05/binding-c-to-lua-using-swig/
希望它可以帮助您入门。
I may be mistaken, but you don't 'pass' an instance of C++ classes to Lua, what you really do is create C++ bindings to Lua whereby you're actually instantiating a C++ class in the Lua script itself.
i.e Say you have a C++ class called Foo with a single method called add(x, y). You can then instantiate and use that class in Lua by doing something like (assuming you've used Swig to create the wrapper file).
I was having some misadventures when using Swig and Lua with my C++ program so I wrote a tutorial (so that I don't forget how I did it). You can view it here... http://glennmccord.wordpress.com/2012/11/05/binding-c-to-lua-using-swig/
Hopefully it can get you started.