4 ms·
Wow, Collect2 sounds kinda hacky based on the linked documentation¹: - There is user code that needs to run before main. With C/C++ allowing separate compilati
by skitter 3y ago
Wow, Collect2 sounds kinda hacky based on the linked documentation¹:
- There is user code that needs to run before main. With C/C++ allowing separate compilation, the compiler invocation that creates the entrypoint doesn't know about all of these static constructors.
- Therefore Collect2 pretends to be ld; when invoked, it tries to call the real ld and examines its output
- It creates a c file with a table containing any constructor symbols it finds, then compiles that
- Finally it links the program again, together with the new object file
¹: https://gcc.gnu.org/onlinedocs/gccint/Collect2.html https://gcc.gnu.org/onlinedocs/gccint/Collect2.html
- fabiensanglard 3y agoThank you for the explanation I did not have the courage to gather.
- ryukoposting 3y agoThe words "undefined reference to '__main'" give me war flashbacks.
- o11c 3y agoI'm pretty sure the documentation actually lies though. Yes, `collect2` exists, but with the existence of all the modern linker sections and scripts, it simply forwards everything to `ld` unchanged. There's no sneaky inspection, C code generation, or second linking pass.
- skitter 3y agoThanks for the clarification. I was questioning my sanity, because collect2 as described in the doc sounds not just hacky, but also like a waste of compile time.
- throw16180339 3y agoIIRC collect2 is only required on a few weird platforms such as AIX.