我正在制作一套框架。我想支持多种编程语言
我正在制作一套框架。
我想支持多种编程语言。
我的问题是我应该使用什么语言作为基础?
有没有办法进行多重绑定?
我想支持所有这些编程语言。
C, C++, Java, Ruby, Perl, Python.
I'm Making a set of frameworks.
I want to support multiple programming languages.
My question is what language should i use as the base?
and is there a way to multiple bindings?
I want to support all these programming languages.
C, C++, Java, Ruby, Perl, Python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将用 C++ 实现该库。如果您需要 C 访问,那么可以通过提供带有附加“this”参数的包装函数来手动轻松完成此操作。
在开始之前,请阅读 SWIG 文档,特别是其限制和需要避免的事项。如果您在设计 C++ 接口时考虑到了 SWIG,您可以毫不费力地获得为您生成的许多语言的绑定。
编辑:这是 C++ 类的 C 包装器的快速示例。假设这是要包装的 C++ 类,我们将其称为
test.h
:这是您的 C 标头
test_c.h
:您的 C 实现将是一个带有以下内容的 C++ 文件 : C 函数,例如
test_c.cpp
:is_valid()
方法可以保证您不会传递错误的句柄。例如,您可以在所有实例中存储幻数,然后is_valid()
只是确保幻数存在。I would implement the library in C++. If you need C access, then this is easily done by hand, by providing wrapper functions that take an additional "this" argument.
Before you start though, read the SWIG documentation, in particular its limitations and things to avoid. If you design your C++ interface with SWIG in mind you can get the bindings for a lot of languages generated for you without effort.
Edit: Here is a quick example of a C wrapper for a C++ class. Let's say this is the C++ class to wrap, let's call it
test.h
:This is your C header
test_c.h
:And your C implementation will be a C++ file with C functions, let's say
test_c.cpp
:The
is_valid()
method is there to guarantee that you are not passed a bad handle. For example, you can store a magic number in all your instances, thenis_valid()
just ensures the magic number is there.查看 GObject — 它是一个开源 C 库,为 C 添加了面向对象的编程功能,并且可以透明地为各种语言创建绑定。
它的同伴 Vala 是一种面向对象的语言,可以编译为 C+GObject 代码,以帮助减少 C+GObject 的冗长。
Check out GObject — it's an open-source C library that adds object-oriented programming functionality for C, and can transparently create bindings for a variety of languages.
And its companion, Vala, is an object-oriented language that compiles down to C+GObject code, to help cut down on the verbosity of C+GObject.
查看 SWIG,“简化的包装器和接口生成器”。给定 C 或 C++ 代码,它可以以编程方式生成各种语言的绑定。
Check out SWIG, "Simplified Wrapper and Interface Generator". Given C or C++ code, it can programmatically generate bindings for a variety of languages.