避免C程序中的主(入口点) [英] Avoiding the main (entry point) in a C program
问题描述
可以避免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 \n");
return 0;
}
int main(void)
{
printf("This is main \n");
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启动例程 - 它做一些设置,然后 调用 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程序中的主(入口点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!