4 ms·
Who Needs Main() Anyway?
- rramadass 4y agoIs this what passes for "technical know-how" nowadays? Just read Hongjiu Lu's classic paper ELF: From The Programmer's Perspective to get an idea of how everything comes together in C/C++ land.
- sargstuff 4y agoBut in today's non-programmer post-covid world, ELF is seasonal, COFF is year round. edit: COFF phoneticly eqivalent to cough.
- jeffreygoesto 4y agoSigh. Wait until she learns about the static initialization order fiasco...
- moloch-hai 4y agoShe knows. Code where it matters is wrong.
- sargstuff 4y agoSounds like a have to C it to believe it.
- deleted 4y ago[deleted]
- xjwm 4y agoI've written code like this in C to create library functions that will be linked in to other projects, but where there is no 'main' function in the C file. Do people use C++ the same way, or is it best practice to include 'main?'
- MathMonkeyMan 4y agoIt's the same as in C. The observation here is that variables with static storage duration can be initialized by running user code before main() begins.
- xjwm 4y agoOh! Thank you. That makes much more sense now.
- colejohnson66 4y agoIt’s also the same in .NET. If your Main() function references types that have a static constructor, the constructors could run before Main(). It’s a surprising thing when one first discovers it, but it makes a lot of sense.
- moloch-hai 4y agoIt didn't crash until after its work was done. It could even have trapped the segfault and exited cleanly.
- marssaxman 4y agoCouldn't just call exit(0)?
- moloch-hai 4y agoMaybe, or _exit().
- 0xcafefood 4y agoA more useful application of this same idea ("you can do stuff during static initialization") is for building a static registry. https://artificial-mind.net/blog/2020/10/17/static-registration-macro https://artificial-mind.net/blog/2020/10/17/static-registrat... gives a good overview.
- sargstuff 4y ago[1] p.99 information might be of interest cc -Wl,-m main.c > main.linker.map related search terms: _crtStartUp crt0 crt1 [1] : https://progforperf.github.io/Expert_C_Programming.pdf https://progforperf.github.io/Expert_C_Programming.pdf