Reputation: 485
I want to generate C wrappers from C++ libraries. There are tutorials on how to do it by hand:
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?
Upvotes: 24
Views: 8043
Reputation: 3454
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
Upvotes: 2
Reputation: 4186
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.
Upvotes: 1
Reputation: 11102
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.
Upvotes: 0
Reputation: 331
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.
Upvotes: 3
Reputation: 11389
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.
Upvotes: 1