将 Lua 编译为 C++ 有什么好处?除了避免“extern C”之外并得到'C++例外?
我对 Lua 还很陌生,我想使用 C++ 将 Lua 嵌入到我们的游戏项目中。我注意到的第一件事是,Lua 被允许编译为 C++ 代码,从文档中,我了解到这将消除围绕 Lua 标头的“extern C”,并且错误处理将是 C++ 异常,而不是跳远/定步跳。
我的问题是,除了这两个区别之外,将 Lua 代码编译为 C++ 代码还有其他真正的好处吗?这两个并不能真正说服我,因为,1)用“extern C”包装 c 头并不困扰我,2)我们的项目不允许异常,所以我必须更改 luaconf.h 才能使用无论如何,长跳/定跳。
I'm quite new in Lua, and I want to embed Lua into our game project using C++. The first thing I notice, Lua is allowed to be compiled as C++ code, and from the doc, I've learned that this will eliminate the 'extern C' wrapping around Lua's headers, and also the error handling will be C++ exception instead of longjump/setjump.
My question is, besides these two differences, is there any other real benefit to compile Lua code as C++ code? These two don't real convince me, as, 1) it doesn't bother me to wrap c headers with 'extern C', 2) our project doesn't allow exception, so I have to change in luaconf.h to use longjump/setjump any way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些就是将 Lua 编译为 C++ 的好处。
extern "C"
根本不是重点;这都是关于异常处理的。虽然您的应用程序禁止异常,但并非每个 C++ 应用程序都这样做。如果您使用全部 C++,您要么必须采取措施防止异常通过 Lua 传递(这不是世界上最简单的事情,除非您使用像 Luabind 这样的包装器),要么编译Lua 作为 C++。
将 Lua 编译为 C++ 没有其他好处。异常处理是Lua可以编译为C++的原因。
Those are the benefits of compiling Lua as C++. The
extern "C"
thing isn't even really the point; it's all about exception handling. And while your application forbids exceptions, not every C++ application does.If you're using all of C++, you either have to take steps to prevent exceptions from passing through Lua (not the easiest thing in the world unless you're using a wrapper like Luabind) or to compile Lua as C++.
There are no other benefits of compiling Lua as C++. Exception handling is the reason why Lua can be compiled as C++.
另一种思考方式可能是把问题反过来问“用 C 语言编译 Lua 有什么好处吗(如果我们已经在使用 C++)?”
如果将 Lua 编译为 C 没有任何优势(据我所知,没有),并且您已经在使用 C++ 来编写其他代码,那么将 Lua 编译为 C++ 似乎也更简单,而且可能更健壮,因为如果从 Lua 到 C++ 的回调碰巧意外抛出,那么你永远不会有非 C++ 堆栈帧来搞砸堆栈倒带...
Another way of thinking about it might be to turn the question on its head and ask "Is there any benefit to compiling Lua in C (if we're already using C++)?"
If there's no advantage to compiling Lua as C—and as far as I know, there isn't—and you're already using C++ for your other code, compiling Lua as C++ too seems both simpler and potentially a little more robust, because you'll never have non-C++ stack frames to screw up stack rewinding if a callback into C++ from Lua happens to throw accidentally...