[Bf-committers] How to mix C and C++ in Blender

Campbell Barton ideasman42 at gmail.com
Wed Jun 19 06:45:08 CEST 2013


On Wed, Jun 19, 2013 at 7:29 AM, Jürgen Herrmann <shadowrom at me.com> wrote:
> Hi Alexander,
>
> I personally don't like this, but it is possible:
>
> http://stackoverflow.com/questions/7281441/elegantly-call-c-from-c
> http://stackoverflow.com/questions/2399344/calling-c-code-from-c
> http://stackoverflow.com/questions/2744181/how-to-call-c-function-from-c
>
> Google is your friend ;) Sometimes...
>
> I don't know exactly what the blender coding conventions say about this, but I would consider this bad style, because you have to introduce wrappers that might easily break when a external lib is updated.
> I am curious what the other devs say ;)
>
> /Jürgen
>
> Am 18.06.2013 um 23:02 schrieb Alexander Pinzon Fernandez <apinzonf at gmail.com>:
>
>> Hi all.
>>
>> I am working on Bmesh operator for perform laplacian editing for sketch
>> mesh gsoc project.
>>
>> If the library to solve the linear system is SuperLU and Eigen3 for matrix
>> operations, then I must learn how to mix C code (SuperLU) and C ++ (Eigen3)
>> in Blender. (elegant and organized way).
>>
>> I have c code file in
>> source\blender\bmesh\operators\bmo_deform_laplacian.c
>> in bmo_deform_laplacian.c i call several c++ functions (ej. au = ftest(4
>> ,5);) declare in bmo_deform_utils.h  and implemented in
>> bmo_deform_utils.cpp.
>>
>> this two files are in source\blender\bmesh\operators\
>>
>> What are the directories where the files should put  bmo_deform_utils.h
>> and bmo_deform_utils.cpp?
>> What are policies and style guides in Blender to mix C code and C ++?
>>
>> Example
>>
>> bmo_deform_laplacian.c
>> //////////////////////////////////////
>> ....
>> #Include "bmo_deform_utils.h"
>> ...
>> au = ftest(4 ,5);
>> ...
>> ///////////////////////////////////////
>>
>> bmo_deform_utils.h
>> ///////////////////////////////////////
>> #ifndef __BMO_DEFORM_UTILS_H__
>> #define __BMO_DEFORM_UTILS_H__
>>
>>
>> #ifdef __cplusplus
>> extern "C" {
>> #endif
>>
>> int ftest(int x, int y);
>>
>> #ifdef __cplusplus
>> };
>> #endif
>>
>> #endif //__BMO_DEFORM_UTILS_H__
>> ///////////////////////////////////////
>>
>> bmo_deform_utils.cpp
>> ///////////////////////////////////////////
>>
>> #include "bmo_deform_utils.h"
>>
>> class ABC{
>> public:
>> int a,b;
>> ABC(int ax, int by ){
>> a = ax;
>> b = by;
>> }
>> int fsum(){
>> return a + b;
>> }
>> };
>>
>> int  ftest(int x, int y){
>> ABC A(x, y);
>> return A.fsum();
>> }
>> ///////////////////////////////////////////
>>
>> Thanks in advance.
>>
>> Att
>> Alexander Pinzon Fernandez

In blender we have a convention of _not_ mixing in C++ with existing C code.

For examples of C code that calls C++ in Blender see:

extern/rangetree/range_tree_c_api.h (used by BMesh)
extern/rangetree/range_tree_c_api.cc

intern/rigidbody/RBI_api.h
intern/rigidbody/rb_bullet_api.cpp

intern/audaspace/intern/AUD_C-API.h
intern/audaspace/intern/AUD_C-API.cpp

-- 
- Campbell


More information about the Bf-committers mailing list