I'd like to use Pure Data as a prototyping tool for my own library. I found out that Pure Data patches are written in C, but my library is written in C++. So how can I use this code in pure data? Since I haven't used plain C, I'd like to know how I could write a C wrapper for C++ classes and how do instantiate my classes then? Or do I have to rewrite everything in C?
4 Answers
You will need to write wrapper functions for every function which needs to be called. For example:
// The C++ implementation
class SomeObj { void func(int); };
extern "C" {
SomeObj* newSomeObj() {return new SomeObj();}
void freeSomeObj(SomeObj* obj) {delete obj;}
void SomeObj_func(SomeObj* obj, int param) {obj->func(param)}
}
// The C interface
typedef struct SomeObjHandle SomeObj;
SomeObj* newSomeObj();
void freeSomeObj(SomeObj* obj);
void SomeObj_func(SomeObj* obj, int param);
Note this must be C++ code. The extern "C"
specifies that the function uses the C naming conventions.

- 43,592
- 5
- 83
- 98
-
7Rather than use `void*` you should use an incomplete struct to gain some type safety. – David Heffernan Oct 07 '11 at 18:08
-
1To help understand better,`extern "Linkage_Specification" { //code }` This way one tells the compiler the Linkage specification to use while linking the code. – Alok Save Oct 07 '11 at 18:10
-
I am not sure I understand your example completly. If I declare those extern "C" functions in my c++ header, why do I have to declare it again in C? Can't I just include the c++ header in my C file and call the functions directly? – Pedro Oct 07 '11 at 18:23
-
If you're going to be writing C code, you need a function declaration for the C compiler to know the declaration of the function. If you're not calling it from C, you don't need the header. And that is a C++ implementation (.cpp), not a header. You can't include the first block of code from C because C doesn't understand classes. – Dark Falcon Oct 07 '11 at 18:31
-
2Typically "extern C" is paired with "#ifdef __cplusplus". See this link: http://dsc.sun.com/solaris/articles/mixing.html – paulsm4 Oct 07 '11 at 18:37
-
4The wrappers should also "eat exceptions" and transform them into return codes. – Matteo Italia Oct 07 '11 at 18:46
-
I understand the part about declaring functions in C (that's just like declaring a method native in java). But how do I link the functions to c? Let's say I have started a new project and added the path to my c++ headers to the include directories. Would this compile without doing anything else, if I adapted your example? – Pedro Oct 07 '11 at 18:48
-
You need to compile the C++ portion into a static or dynamic library and add it to the list of libraries you link against in your C program. – Dark Falcon Oct 07 '11 at 19:16
You can also write objects for Pure Data using C++ using the flext framework.

- 2,572
- 31
- 30
Let me put it another way:
1) You can call C functions, data and libraries from C++ source, and you call C++ source from C.
2) Whenever C calls into C++ source, however, that source must be written in a C subset of C++.
3) Part of this is using "extern C".
4) Another part is using "#ifdef __cplusplus"
5) The links I cited above give plenty of details
6) I looked at the Pure Data site. You might have to make some "tweaks" to your library. You might wish to create a new header. But fundamentally, I think you can probably accomplish your goal of getting your library to integrate with Pure Data.
IMHO...

- 114,292
- 17
- 138
- 190
You can absolutely call C from C++ - no problemo!
Worst case, you might have to explicitly declare those functions you call from Pure Data as "extern C". But it's almost certain that Pure Data has already done that for you (you'll probably see "extern C" in the Pure Data header files.
Here's more info:
http://msdn.microsoft.com/en-us/library/0603949d%28v=vs.80%29.aspx
'Hope that helps!

- 114,292
- 17
- 138
- 190
-
1I think from the Q the OP wants to call C++ from C, not the other way round. – Alok Save Oct 07 '11 at 17:59
-
-