Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Who Needs Main() Anyway? (rachelbythebay.com)
30 points by HieronymusBosch on Jan 21, 2023 | hide | past | favorite | 14 comments


[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


Is 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.


But in today's non-programmer post-covid world, ELF is seasonal, COFF is year round.

edit: COFF phoneticly eqivalent to cough.


A 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-registrat... gives a good overview.


I'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?'


It'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.


It’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.


Oh! Thank you. That makes much more sense now.


Sigh. Wait until she learns about the static initialization order fiasco...


She knows. Code where it matters is wrong.


Sounds like a have to C it to believe it.


It didn't crash until after its work was done. It could even have trapped the segfault and exited cleanly.


Couldn't just call exit(0)?


Maybe, or _exit().




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

Search: