Products > Programming

Crazyness: C and C++ mixing

(1/14) > >>

netmonk:
Hello there.

I try to stick to esp-idf and play with lora rf module.
I know C.
But All the libraries i can find are made in C++.

Trying to mix C and C++ looks like a wild adventure and i dont want to spend time learning C++.

What are you doing in such situation ?
Im just an amateur hobbyist, im quite confused. Is it something usual in MCU world ?

tszaboo:
You can use all the features of C in C++.
You don't need to use the introduced features of C++.
Unless a library forces you to use them.
String manipulation is a lot easier in C++ so even if you just use those parts, you might be ahead.

sleemanj:
Take your .c file, rename it to .cpp, the end (for the most part).

netmonk:
well is it very expensive to transform a c++ library into c ?

newbrain:

--- Quote from: netmonk on June 09, 2021, 09:14:30 am ---well is it very expensive to transform a c++ library into c ?

--- End quote ---
Short answer: it depends.

Slightly longer answer: it depends a lot on how the library is written.

Slightly long answer:
There are two aspects, in general, to a library: its interface and its implementation.
If the interface is class based, with ample use of inheritance, abstractions, or templates the task is very expensive.
A nice design in C++ (e.g. using a template to instantiate and use a specific GPIO port) would take a lot of effort to translate, and the result will probably look contrived and ugly.
The same can be said for the implementation, even though the low levels often tend to look more like C with classes, than real C++.

That said, many of the C++ library one finds on the net, especially Arduino crap, are C++INO (C++ in name only).


--- Quote from: sleemanj on June 09, 2021, 08:39:12 am ---Take your .c file, rename it to .cpp, the end (for the most part).

--- End quote ---
But pay attention to all the 'minor' differences that will come back to bite your ass (e.g. const, enums etc...).

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod