Why is C++ used in Blender? Why not just use C everywhere?

Hello devs, so I looked into Blender’s code recently and found that C++ is being used in some areas. Would you mind telling a noob why this is the case? I just want to understand so I can know if C is all I need if I want to code certain parts of Blender and not others.
Also, why would the use of C and C++ be essential in developing other open source or cross-platform software?

C++ has many features that are useful and can make it easier to write certain code. This is a matter of taste to some extent, but many Blender developers prefer writing code in C++ over C.

If the area you want to work on is written in C++, you’ll need to learn it.

There are other programming languages that can be used for open source and cross-platform software, it’s just that C/C++ was chosen for Blender a long time ago and that’s what we stick to.

2 Likes

I’d very much like to be able to code Blender’s UI and Viewport. Sorry for being annoyingly ignorant, but Which is (are) needed for these two areas?

The problem is there isn’t this sort of info in the documentation. I’m sure many people would love to know exactly what programming language(s) to learn and code areas of Blender they like.

The UI and viewport are in C. You can tell by the file extensions in each module, if it’s .c it’s C code, if it’s .cc or .cpp it’s C++ code.

And then you can check here for which modules contain which functionality:
https://wiki.blender.org/wiki/Source/File_Structure

1 Like

Oh man… Thanks a million! You’re the man! :grin:

1 Like

I think there are also a few libraries that interface better with C++, but that’s a guess. I’m sure most of the cross-platform open source libraries that are widely adopted in the industry are C++ – Alembic and OpenVDB are two such examples. The Boost library is C++, and if I’m not mistaken it’s used extensively in Cycles. Of course, a C++ library may be compatible with C and a C library can be compatible with C++, but it can be easier to interface with a library in its own language, especially if the library must be modified for Blender in one area or another (for example, the improvements to MantaFlow that have originated with a bugfix in Blender).
I don’t know a lot about the code, so take this with a grain of salt. If you want to work on viewport code it might also be a good idea to know GLSL, since IIRC the viewport shaders are GLSL fragment shaders that are generated by C code (again, could be wrong).