Why does static library contain a main function?

ε祈祈猫儿з 提交于 2021-02-07 02:56:24

问题


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?


回答1:


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.




回答2:


I just wonder what the purpose it is.

It's a common technique with unit testing or graphics/game engine frameworks to define the main() entry point of the executable program, and binding the custom class definitions from certain factory pattern templates.

how does the main() execute?

It is the main entry point of any c++ program by definition, so the execution is triggered by the program start up linker script.


Using such stuff means you write your client classes in an executable project, bind them with the framework, and omit to define a main() function.



来源:https://stackoverflow.com/questions/40514935/why-does-static-library-contain-a-main-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!