Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> this just hasn't been the case for some time now

Which I find sad actually. The idea of C++ as a superset of C is really powerful, especially when mixing C and C++. A while ago I had a C project (firmware for a microcontroller) and wanted to bake the version and the compilation time into the firmware. I didn't find a way to do this in plain C, but in C++ you can initialize a global struct and it gets statically linked into the output. This didn't even use constexpr, just preprocessor trickery. Then it was just a matter of renaming the c file to cpp and recompiling. I guess you could also do that with C, but there are things like RAII or constexpr or consuming a C++ library that you can't do without.



> wanted to bake the version and the compilation time into the firmware. I didn't find a way to do this in plain C, but in C++ you can initialize a global struct and it gets statically linked into the output. This didn't even use constexpr, just preprocessor trickery.

I might be misunderstanding here, but if you are okay with preprocessor trickery, then it's doable.

I do this routinely in the Makefile, which (very tediously) generates a build_info module (header and implementation) that is linked into the final binary: https://github.com/lelanthran/skeleton-c/blob/8e04bed2654dac...


> I didn't find a way to do this in plain C

Not sure what you were running into. I routinely do this just fine.

> This didn't even use constexpr, just preprocessor trickery.

Isn't the preprocessor shared between C and C++?

> in C++ you can initialize a global struct and it gets statically linked into the output

That sounds to be doable just the same in C?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: