Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C++ API in C?

One day I decided to start writing a video game in plain old C.
It was a lot of fun, and three months later (I sometimes have little time away from the job) I found myself in the need of some physics engine.
I decided to use Bullet physics engine, because it seems like one of the better ones out there for what I need.
Then, I found out Bullet doesn't really have a C API but only a full C++ API. Its C API is not maintained.
After a day of cursing, I 'converted' my project into C++, which is a bold statement for saying I typecasted all heap allocation and I use new and delete instead of malloc and free, and wrapped up some definitions in 'extern "C" { ... }'.
Some people would probably shoot me for doing that, but I saw no other option to using a performance-tasking thing such as this physics engine, which only has a C++ API, in C.

So now, I'm compiling with g++, while still writing mostly "C" code. I find myself a little less happy, because the code no longer feels as pure.
C++ gives me some strange error messages, while I have nothing against the language I often do not like the g++ parser. Aside from the fact that I can now happily bounce objects into each other, some of the smallness and purity of my pet project has been deserted now.

I'm wondering if I did the right thing. Can I ask for some advice, should I just carry on and not worry about using a C++ compiler for my 'mostly' C code? Are there other ways to use this API in C without any performance hits or overdone maintenance work?

like image 927
buddhabrot Avatar asked Dec 04 '22 23:12

buddhabrot


2 Answers

I'm wondering if I did the right thing.

Well, you needed a component for your project, and instead of inventing it again from scratch, you reused an existing piece of software. It actually sounds right to me.

Can I ask for some advice, should I just carry on and not worry about using a C++ compiler for my 'mostly' C code?

You don't need to worry at all. C++ is almost 100% C compatible. There are no penalties on doing that. You have actually earned better compile-time checkings due to a much stricter type system.

Are there other ways to use this API in C without any performance hits or overdone maintenance work?

No, you can't use the API in C without converting your code to C++. C++ identifiers are mangled: this means that, even if you were ready to use the C++ API from plain C, you wouldn't know how to refer to them. Theoretically this is possible, in practice it isn't.

Your way is the correct one. Good luck with that game!

like image 93
Baltasarq Avatar answered Dec 09 '22 14:12

Baltasarq


I think you're overly concerning yourself with things that aren't really problems. You say that some of the purity has gone. Well, I can understand this. You wrote it in C and then, realizing you had to use C++ to use an API of choice, shoehorned it in. Now it doesn't feel like your pet baby anymore. This is a subconscious issue, not a programming one.

I suggest you bite the bullet (haha), and rewrite your project in C++. Then it will be pure again, you'll learn a lot on the way, and won't feel like your C-child is being sodomized.

like image 32
Moo-Juice Avatar answered Dec 09 '22 13:12

Moo-Juice