Using C ++ API in C?

One fine day, I decided to start writing a video game in plain old C.
It was a lot of fun, and three months later (sometimes I have little time from work) I found myself in the need for some kind of physical engine.
I decided to use the Bullet physics mechanism, because it seems to be one of the best that I need.
Then I found out that Bullet does not actually have a C API, but only a complete C ++ API. Its C API is not supported.
After a day of damnation, I “converted” my project to C ++, which is a bold statement in order to say that I attributed the entire heap distribution, and I use new and delete instead of malloc and free, and wrapped some of the definitions in “extern” C "{...} '.
Some people would probably shoot me for this, but I have not seen another use case for a task like this physics engine, which only has a C ++ API, in C.

So now I am compiling with g ++, still writing mostly "C" code. I feel a little less happy because the code no longer feels just as clean.
C ++ gives me some weird error messages, while I have nothing against a language that I often don't like the g ++ parser. Besides the fact that now I can happily bounce objects to each other, some of the smallness and purity of my pet project are now empty.

I am wondering if I did the right thing. Can I ask for advice, should I just go ahead and not worry about using the C ++ compiler for my “mostly” C code? Are there other ways to use this API in C without any performance failures or excessive maintenance work?

+4
source share
7 answers

I am wondering if I did the right thing.

Well, you need a component for your project, and instead of reinventing it again from scratch, you reused existing software. It really sounds to me.

Can I ask for advice if I just go ahead and don’t worry about using the C ++ compiler for my “mostly” C code?

You do not need to worry. C ++ is almost 100% compatible. No fines are charged on this. In fact, you have earned better compile-time checks due to the much more stringent type system.

Are there other ways to use this API in C without any results or exaggerated repair work?

No, you cannot use the API in C without converting your code to C ++. C ++ identifiers are distorted: this means that even if you were ready to use the C ++ API from simple C, you would not know how to access them. Theoretically, this is possible, in practice it is not.

Your path is right. Good luck with this game!

+4
source

I think that you are over-treating yourself with things that are not really problems. You say that some kind of purity is gone. Well, I can understand that. You wrote it in C, and then, realizing that you need to use C ++ to use the selected API, decrypt it. Now this is not like your beloved child. This is a subconscious problem, not a programming one.

I suggest you bite a bullet (ha ha) and rewrite your project in C ++. Then it will become clean again, you will learn a lot along the way and you will not feel that your C-child is sodomized.

+3
source

Depending on how closely related different subsystems of your engine are, it may be possible to write different parts of the engine in different languages.

For example, you can divide the physical part into a C ++ module that exports the C API and leave your application in C. However, it may make sense to write the central part of the system in C ++ and reorganize the existing C code into separate modules.

+2
source

Don't worry about writing mostly C code and compiling it with C ++. Just accept the use of new / delete instead of malloc / free and everything will be fine. The advantage of using the new one is that you do not need to cast the result to the correct pointer type (implicit casts from void * to other pointers are forbidden in C ++), and that the new one will never return an invalid pointer. You will lose realloc (). (And no, don't even think about mixing new / delete with realloc :))

+1
source

If you want to write your project in C, write it in C and make a C-shell for the library; look at this question for some tips on this.

Otherwise, rewrite your project in C ++; but please don’t write another C project with some C ++ here and there, in my experience these projects can quickly become a mess (and if you write code with C thinking, but call C ++ code, then strange things begin to arise when exceptions are added to the mix).

+1
source

The only reasons you could save the project as “pure C” are either because of source code compatibility, tool support, or because of language standards (MISRA-C, etc.). “I want him to feel clean,” is probably not a very rational argument.

If you save code like pure C for such reasons, you could write a “shell” of the DLL (assuming Windows) in C ++, which makes all communication with your third-party API. Of course, you get a little overhead.

Strange error messages are undoubtedly caused by more severe typing in C ++, which can be a blessing or a scourge, depending on the situation. The C ++ compiler is more likely to hit with its fingers when it encounters dangerous implicit types (integer promotions, etc.), and this is likely to lead to a tightening of the "correctness of the competition." But at the same time, he will moan about void pointers, which are considered good general C programming, but dangerous, messy, and possibly redundant in C ++.

+1
source

One option is to convert C ++ code to C using LLVM. See this FAQ on the project website: Can I convert C ++ code to C code?

0
source

Source: https://habr.com/ru/post/1339997/


All Articles