为什么静态库包含主函数? [英] Why does static library contain a main function?

查看:141
本文介绍了为什么静态库包含主函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的静态库,其中包含一个 main() 函数(C ++).
我只是想知道它的目的是什么.

I came across a weird static library which contains a main() function (C++).
I just wonder what the purpose it is.

main()如何执行?

推荐答案

从链接器角度看, main 函数的位置并不重要-它可以在静态库中,也可以在静态库中在独立的目标文件中,链接器不在乎.无论目标文件来自何处,它都会从目标文件中生成可执行文件,并且在最终的可执行文件中,库/非库符号之间的所有区别都将丢失.

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.

为此,我可以想象某种专门的应用程序框架可以在库中具有main,而您可以以已定义函数的形式为其提供回调.

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.

这篇关于为什么静态库包含主函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆