避免在C程序中的main(入口点) [英] Avoiding the main (entry point) in a C program
问题描述
是否有可能避免在C程序的入口点(主)。在下面的code,是有可能调用 FUNC()
呼叫而不通过调用main()
在下面的程序?如果是,如何做到这一点,当它会被要求,为什么被这样的规定给定的?
INT FUNC(无效)
{
的printf(这是FUNC \\ n);
返回0;
}INT主要(无效)
{
的printf(这是主\\ n);
返回0;
}
如果你正在使用gcc,我发现一个线程,说你可以使用的 -e
命令行参数来指定不同的切入点;所以你可以使用 FUNC
作为你的切入点,这将使主
未
请注意,这实际上并没有让你调用其他程序,而不是主
。相反,它可以让你调用其它程序而不是的_start
,这是libc的启动程序 - 它一些设置,然后的是的通话主
。所以,如果你这样做,你就会失去一些内置到您的运行时库初始化code,这可能包括像解析命令行参数。在使用它之前阅读了这个参数。
如果你使用其他的编译器,有可能会或可能不会是这个参数。
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 \n");
return 0;
}
int main(void)
{
printf("This is main \n");
return 0;
}
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.
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屋!