Jeffery Wang Jeffery Wang - 26 days ago 16
C++ Question

Why does static lib contain main function

I came across a weird static library which contains a

main()
function(c++).

I just wonder what the purpose it is.

How does the
main()
execute?

Answer

From the linker perspective, it doesn't matter where the main function is - it can be in a static library as well as in standalone object file, linker couldn't care less. It produces the executable from object files, no matter where they come from, and in the final executable all the distinction between library/non library symbols is lost.

As for the purposes, i can imagine that some sort of specialized application framework could have main in the library, with you providing callbacks to it in form of defined functions.