从 C++ 生成 C 包装器?
我想从 C++ 库生成 C 包装器。 有关于如何手动执行此操作的教程:
- http://dsc.sun.com/ Solaris/articles/mixing.html
- http: //www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
但这是太多的体力劳动。
例如,为此:
struct RtAudio {
virtual DeviceInfo const& f() {...}
class DeviceInfo {
virtual void g() { ... }
};
...
};
我需要写:
struct RtAudioC {
RtAudio x;
};
struct DeviceInfo {
RtAudio::DeviceInfo x;
};
extern "C" {
RtAudioC* newRtAudio() {
return new RtAudioC;
}
void deleteRtAudio(RtAudioC *p {
delete p;
}
/* do something with RtAudio::f() */
void g(DeviceInfo *p) {
try {
p->x.g();
} catch (SomeError & err) {
}
}
}
是否有可以自动化此过程的工具?
I want to generate C wrappers from C++ libraries.
There are tutorials on how to do it by hand:
- http://dsc.sun.com/solaris/articles/mixing.html
- http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
But it is too much of a manual labor.
For example, for this:
struct RtAudio {
virtual DeviceInfo const& f() {...}
class DeviceInfo {
virtual void g() { ... }
};
...
};
I need to write:
struct RtAudioC {
RtAudio x;
};
struct DeviceInfo {
RtAudio::DeviceInfo x;
};
extern "C" {
RtAudioC* newRtAudio() {
return new RtAudioC;
}
void deleteRtAudio(RtAudioC *p {
delete p;
}
/* do something with RtAudio::f() */
void g(DeviceInfo *p) {
try {
p->x.g();
} catch (SomeError & err) {
}
}
}
Are there tools that can automate this process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以试试SWIG,C代码生成器是去年的GSoC项目。 AFAIK 他们还没有将它合并到主干中,所以你必须结帐并查看它。 从 SVN 构建分支。
You can try SWIG, C code generator was last year's GSoC project. AFAIK they haven't merged it to the trunk yet, so you'd have to checkout & build the branch from SVN.
我刚刚在 Python 中开发了一个 C 函数包装器来完成此任务(生成包装 C 函数的 C++ 类)。
它还很年轻,但我需要它做的大部分事情现在都在那里。 尝试一下,让我知道您的想法:https://github.com/mlb5000/CFunctionWrapperGenerator
I just developed a C function wrapper in Python to do exactly this (generate C++ classes that wrap C functions).
It's still young but the bulk of what I needed it to do is in there now. Give it a try and let me know what you think: https://github.com/mlb5000/CFunctionWrapperGenerator
gmmproc 可以为基于 gobject 的 C 库创建 C++ 包装器,但这是我听说过的唯一介于 C 和 C++ 之间的代码生成器。
如果您擅长编写解析器,那么创建基本的包装器生成器并不是一项太困难的任务。 最后你可能需要手动添加一些收尾工作,但仍然会减少你的工作量。
There is gmmproc which creates C++ wrappers for gobject based C libraries, but that's the only code generator I've heard of between C and C++.
If you're good with writing a parser, it wouldn't be too difficult a task to create a basic wrapper generator. In the end you might have to add a few finishing touches manually, but still your work load would be reduced.
您已经编写了多少 C++ 代码,还有多少尚未编写?
如果要编写合理的比例,我将创建一个简化的语法,它生成 C++ 和 C 标头,就像 IDL 对 COM 接口所做的那样。 这种简化的语法比 C++ 更容易解析,或者您可能会找到现成的东西可以做到这一点。
How much of your C++ code is already written vs. how much has yet to be written?
If a reasonable proportion is to-be-written, I would create a simplified syntax, that generates both the C++ and C headers, like IDL does for COM interfaces. This simplified syntax will be much easier for you to parse than C++, or you can likely find something off the shelf that does this.
我不知道有现成的工具可以做到这一点。 如果您想自动生成并乐意编写自己的脚本,pygccxml (基于 GCC-XML)是解析 C++ 标头的好方法。
I don't know of an off-the-shelf tool to do this. If you want to automate the generation and are happy to write your own scripts, pygccxml (based on GCC-XML) is quite a nice way to parse C++ headers.