Search code examples
c++static-librariesprogram-entry-point

Why does static library contain a 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?


Solution

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