5 ms·
For people advocating use of C++ instead of C, keep in mind there are several platforms (mostly embedded) that only support C and not C++. Also there are many p
by israrkhan 4y ago
For people advocating use of C++ instead of C, keep in mind there are several platforms (mostly embedded) that only support C and not C++. Also there are many projects that make use of C only. If C++ is available, I agree one should use it, however that is not always the choice.
- cozzyd 4y agoWhen working on some platforms (e.g. BeagleBoneBlack), the compilation time of C++ is a huge turnoff. Sure, you can cross-compile, but that feels like even more friction.
- NavinF 4y agoWait you ssh into a BeagleBoneBlack and compile C on the device? I’ve seen some janky envs in my life but this is pretty high up. Do you never enable LTO when linking deps? These devices are literally 1000x slower than a typical 8 core desktop.
- cozzyd 4y agoYes, and edit/debug code in the device too. Setting up an identical cross compilation environment with all the same shared libraries (it's a full Linux system after all) and such is non-trivial and C compiles fast enough it's fine... (1000 times is a bit of an exaggeration anyway...) Now maybe if I were an emacs user this setup wouldn't work...
- megous 4y agoCompiling a single C file program, linking glib, libarchive and some other stuff (repeated runs, to allow for caching inputs): Orange Pi 3: real 0m0.916s user 0m0.839s sys 0m0.074s 5950X real 0m0.080s user 0m0.064s sys 0m0.016s Slower, but not nearly 1000x in practice. Cortex-A8 is a bit slower, still, but not by much.
- cozzyd 4y agowell, one project I work on compiles ~100 times faster on my desktop (Ryzen 5 3600) than the BBB if building from scratch, and that's not accounting for multiple cores, so 1000x might be reasonable for a project with sufficient compile-time parallelization. But it's still fast enough (< 10 seconds ) on the BBB that setting up a cross-compilation environment isn't worth it (and obviously incremental compiles are faster).
- bsder 4y ago> Wait you ssh into a BeagleBoneBlack and compile C on the device? Exactly how am I supposed to access the hardware that only exists on the Beaglebone Black itself, otherwise? You can cross-compile all day, but, in the end, I need to code to be running over there to toggle those GPIOs. I need the debugging to be over there. I need VSCode to be running over there. Not all programming is web programming.
- cozzyd 4y agoI mean, theoretically it's possible to set up a cross-compilation environment (which would require mirroring basically the entire /usr of the BBB) and copy over compiled files for running / debugging. But it's only worth it if compiling takes a long time (or I suppose if you were to use templaty C++ or rust or something, you may not have enough memory on the BBB to actually compile things... but maybe don't do that?).
- bsder 4y agorust actually does okay on the BBB unless you activate heavy macros at which point the memory blows up, and it grinds to a halt. rust-analyzer, for example, could be built (slowly) on the BBB until recently. The last refactoring of it ... ooof ... did something to macros and now the memory footprint is gargantuan.
- DeathArrow 4y ago> If C++ is available, I agree one should use it, C is very simple, while C++ is a monster with 100 heads, each speaking in another language. If all members of a project use the same subset of C++, then yes, it would be preferable over C. But if anyone or any team in a large project uses whatever he wants, things can get pretty complicated, pretty fast.
- pjmlp 4y agoIf the team doesn't care about code reviews, static analysis and automatic formating tools on PR merges, surely.
- numeromancer 4y agoIn C, whenever any part of the creature needs to do some thinking, it grows its own small head to do just enough thinking to get by, and then goes about its business. The process is simple, but the result is often Cthulhic.
- gorgoiler 4y agoEmbedded devices really opened my eyes to this world. What a fool I was using malloc on my first Arduino project.
- jcelerier 4y agoArduino is entirely C++ though
- lelanthran 4y ago> Arduino is entirely C++ though No, it's C++ without exceptions (so constructors are useless). On Arduino you will almost certainly program in a C-with-classes manner; lack of exceptions make almost everything that C++ brings to the table useless[1].
- jandrewrogers 4y agoIt is remarkable how many large and modern C++ code bases somehow make thorough use of the language, including constructors, without exceptions. Unless the asserted need for exceptions is a gross mischaracterization.
- jcelerier 4y ago> lack of exceptions make almost everything that C++ brings to the table useless[1]. sorry what ? constexpr (https://github.com/arduino-libraries/Arduino_EdgeControl/blob/48858179293228a84c323184be7c148303d2e81f/examples/Basic/0-5V_Input/0-5V_Input.ino https://github.com/arduino-libraries/Arduino_EdgeControl/blo...), templates (https://github.com/arduino-libraries/Temboo/blob/dcce3007eb5158d9360fd56531e7d8d22b3c5f5c/src/utility/MQTTClient.h https://github.com/arduino-libraries/Temboo/blob/dcce3007eb5...), etc. are all widely used in the Arduino community. I've recently done some work involving C++20 coroutines on an RP2040. etc etc... And even without exceptions, RAII can still help ensuring a large amount of invariants automatically.
- pjmlp 4y agoUnless we are talking about something like Z80, 6502, PIC, AVR and similar low end CPUs, which hardly full support even C89, all modern CPUs have C++ compilers available. More often than not, it is a matter of not wanting to use C++ than it not being available.
- dnet 4y agoWhy do you mention AVR? AVR-GCC has C++ support, that's what made the original Arduino (before they switched to ARM) approachable to beginners.
- pjmlp 4y agoList of typical 8 bit CPUs, and I was thinking only about commercial compilers.
- lelanthran 4y ago> AVR-GCC has C++ support It's a very restricted subset of C++ that has almost no advantages over plain C, and some number of disadvantages.
- pjmlp 4y ago- templates instead of macros - constexpr instead of macros - if constexpr instead of macros - stronger type checking - type inference - strong type enumerations - namespacing instead of pre-historic naming prefixes - classes as means to enforce type invariants I see some advantages.
- lelanthran 4y ago> More often than not, it is a matter of not wanting to use C++ than it not being available. That's perfectly valid. I reach for C when I need to write fairly small programs. When C does not fill my needs anymore, I don't reach for C++. There are better high-level languages. C++ is not a replacement for C. When one of the worlds foremost experts on C++ complains that the language is too complex to understand[1], passing on C++ is a reasonable position. [1] https://steven.brokaw.org/posts/scott-meyers-cant-remember-cpp-intricacies https://steven.brokaw.org/posts/scott-meyers-cant-remember-c...
- Kab1r 4y agoSomehow I enjoy writing C much more than C++