避免 C 程序中的 main(入口点) [英] Avoiding the main (entry point) in a C program

查看:16
本文介绍了避免 C 程序中的 main(入口点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以避免 C 程序中的入口点(main).在下面的代码中,是否可以在下面的程序中调用 func() 调用而不通过 main() 调用?如果是,如何做,什么时候需要,为什么要给出这样的规定?

Is it possible to avoid the entry point (main) in a C program. In the below code, is it possible to invoke the func() call without calling via main() in the below program ? If Yes, how to do it and when would it be required and why is such a provision given ?

int func(void)
{
     printf("This is func 
");
     return 0;
}

int main(void)
{
     printf("This is main 
");
     return 0;
}

推荐答案

如果你使用 gcc,我发现一个线程说你可以使用 -e 命令行参数 指定不同的入口点;所以你可以使用 func 作为你的入口点,这将使 main 未被使用.

If you're using gcc, I found a thread that said you can use the -e command-line parameter to specify a different entry point; so you could use func as your entry point, which would leave main unused.

请注意,这实际上并不允许您调用另一个例程而不是 main.相反,它允许您调用另一个例程而不是 _start,这是 libc 启动例程——它进行一些设置,然后 it 调用 main.因此,如果您这样做,您将丢失一些内置在运行时库中的初始化代码,其中可能包括解析命令行参数之类的内容.使用前请阅读此参数.

Note that this doesn't actually let you call another routine instead of main. Instead, it lets you call another routine instead of _start, which is the libc startup routine -- it does some setup and then it calls main. So if you do this, you'll lose some of the initialization code that's built into your runtime library, which might include things like parsing command-line arguments. Read up on this parameter before using it.

如果您使用的是其他编译器,则可能有也可能没有此参数.

If you're using another compiler, there may or may not be a parameter for this.

这篇关于避免 C 程序中的 main(入口点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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