Can the main()
function be declared static
in a C program? If so then what is the use of it?
main()函数可以在C程序中声明为静态的吗?如果是这样,那么它有什么用呢?
Is it possible if I use assembly code and call the static main()
function myself (consider embedded programs)?
如果我使用汇编代码并自己调用静态main()函数(考虑嵌入式程序),是否可能?
5 个解决方案
#1
33
No. The C spec actually says somewhere in it (I read the spec, believe it or not) that the main function cannot be static.
不。C规范实际上在其中的某个地方(我读过规范,信不信由你)说主函数不能是静态的。
The reason for this is that static means "don't let anything outside this source file use this object". The benefit is that it protects against name collisions in C when you go to link (it would be bad bad bad if you had two globals both named "is_initialized" in different files... they'd get silently merged, unless you made them static). It also allows the compiler to perform certain optimizations that it wouldn't be able to otherwise. These two reasons are why static is a nice thing to have.
原因是静态意味着“不要让源文件之外的任何东西使用这个对象”。好处是,当您访问链接时,它可以防止C中的名称冲突(如果在不同的文件中有两个全局变量都名为“is_initialized”,那就太糟糕了……它们会无声地合并,除非你让它们保持静止)。它还允许编译器执行某些优化,否则它将无法执行这些优化。这两个原因就是为什么静态是一个好东西。
Since you can't access static functions from outside the file, how would the OS be able to access the main function to start your program? That's why main can't be static.
既然您不能从文件外部访问静态函数,那么操作系统如何能够访问主函数来启动您的程序呢?这就是为什么main不能是静态的。
Some compilers treat "main" specially and might silently ignore you when you declare it static.
有些编译器专门处理“main”,当您声明它为静态时,它们可能会默默地忽略您。
Edit: Looks like I was wrong about that the spec says main can't be static, but it does say it can't be inline in a hosted environment (if you have to ask what "hosted environment" means, then you're in one). But on OS X and Linux, if you declare main static, then you'll get a link error because the linker can't find the definition of "main".
编辑:看起来我错了,规范说main不能是静态的,但是它说它不能在托管环境中内联(如果你必须问“托管环境”是什么意思,那么你就在其中)。但是在OS X和Linux上,如果声明main static,就会出现链接错误,因为链接器无法找到“main”的定义。
#2
9
You could have a static function called main()
in a source file, and it would probably compile, but it would not be the main()
function because it would be invisible to the linker when the start-up code (crt0.o on many (older) Unix systems) calls main()
.
在源文件中可以有一个名为main()的静态函数,它可能会编译,但它不会是main()函数,因为在启动代码(crt0)时,链接器不会看到它。o在许多(较老的)Unix系统上调用main()。
Given the code:
考虑到代码:
static int main(int argc, char **argv)
{
return(argv + argc);
}
extern int x(int argc, char **argv)
{
return(main(argc, argv));
}
GCC with -Wall helpfully says:
用-Wall很有帮助地说:
warning: 'main' is normally a non-static function
Yes, it can be done. No, it is normally a mistake - and it is not the main()
function.
是的,这是可以做到的。不,这通常是一个错误——它不是main()函数。
#3
2
No you cannot do it. If you will do it you will be unable to compile your program. Because static function is only visible within the same file, so the linker will no be able to find it and make a call of it.
不,你做不到。如果你这样做,你将无法编译你的程序。因为静态函数只在同一个文件中可见,所以链接器将无法找到它并调用它。
#4
0
As others have said, no it can't. And that goes double if you ever intend to port your code to C++, as the C++ Standard specifies that main() need not actually be a function.
正如其他人所说,不,它不能。如果您打算将代码移植到c++,那么这一数字将增加一倍,因为c++标准指定main()实际上不需要是函数。
#5
0
C has two meanings for 'static'...
C有两个意思表示“静态”……
static for a local variable means it can be used globally. static for a global variable means is can only be used in the current file.
对于局部变量来说,静态意味着可以全局地使用它。全局变量的静态方法只能在当前文件中使用。
static for functions has the exact same impact as denoting a global variable as static ... the static function IS ONLY VISIBLE IN THE CURRENT FILE ...
函数的静态影响与将全局变量表示为静态的影响完全相同。静态函数仅在当前文件中可见。
Thus main can NEVER be static, because it would not be able to serve as the primary entry point for the program.
因此main不能是静态的,因为它不能作为程序的主要入口点。
#1
33
No. The C spec actually says somewhere in it (I read the spec, believe it or not) that the main function cannot be static.
不。C规范实际上在其中的某个地方(我读过规范,信不信由你)说主函数不能是静态的。
The reason for this is that static means "don't let anything outside this source file use this object". The benefit is that it protects against name collisions in C when you go to link (it would be bad bad bad if you had two globals both named "is_initialized" in different files... they'd get silently merged, unless you made them static). It also allows the compiler to perform certain optimizations that it wouldn't be able to otherwise. These two reasons are why static is a nice thing to have.
原因是静态意味着“不要让源文件之外的任何东西使用这个对象”。好处是,当您访问链接时,它可以防止C中的名称冲突(如果在不同的文件中有两个全局变量都名为“is_initialized”,那就太糟糕了……它们会无声地合并,除非你让它们保持静止)。它还允许编译器执行某些优化,否则它将无法执行这些优化。这两个原因就是为什么静态是一个好东西。
Since you can't access static functions from outside the file, how would the OS be able to access the main function to start your program? That's why main can't be static.
既然您不能从文件外部访问静态函数,那么操作系统如何能够访问主函数来启动您的程序呢?这就是为什么main不能是静态的。
Some compilers treat "main" specially and might silently ignore you when you declare it static.
有些编译器专门处理“main”,当您声明它为静态时,它们可能会默默地忽略您。
Edit: Looks like I was wrong about that the spec says main can't be static, but it does say it can't be inline in a hosted environment (if you have to ask what "hosted environment" means, then you're in one). But on OS X and Linux, if you declare main static, then you'll get a link error because the linker can't find the definition of "main".
编辑:看起来我错了,规范说main不能是静态的,但是它说它不能在托管环境中内联(如果你必须问“托管环境”是什么意思,那么你就在其中)。但是在OS X和Linux上,如果声明main static,就会出现链接错误,因为链接器无法找到“main”的定义。
#2
9
You could have a static function called main()
in a source file, and it would probably compile, but it would not be the main()
function because it would be invisible to the linker when the start-up code (crt0.o on many (older) Unix systems) calls main()
.
在源文件中可以有一个名为main()的静态函数,它可能会编译,但它不会是main()函数,因为在启动代码(crt0)时,链接器不会看到它。o在许多(较老的)Unix系统上调用main()。
Given the code:
考虑到代码:
static int main(int argc, char **argv)
{
return(argv + argc);
}
extern int x(int argc, char **argv)
{
return(main(argc, argv));
}
GCC with -Wall helpfully says:
用-Wall很有帮助地说:
warning: 'main' is normally a non-static function
Yes, it can be done. No, it is normally a mistake - and it is not the main()
function.
是的,这是可以做到的。不,这通常是一个错误——它不是main()函数。
#3
2
No you cannot do it. If you will do it you will be unable to compile your program. Because static function is only visible within the same file, so the linker will no be able to find it and make a call of it.
不,你做不到。如果你这样做,你将无法编译你的程序。因为静态函数只在同一个文件中可见,所以链接器将无法找到它并调用它。
#4
0
As others have said, no it can't. And that goes double if you ever intend to port your code to C++, as the C++ Standard specifies that main() need not actually be a function.
正如其他人所说,不,它不能。如果您打算将代码移植到c++,那么这一数字将增加一倍,因为c++标准指定main()实际上不需要是函数。
#5
0
C has two meanings for 'static'...
C有两个意思表示“静态”……
static for a local variable means it can be used globally. static for a global variable means is can only be used in the current file.
对于局部变量来说,静态意味着可以全局地使用它。全局变量的静态方法只能在当前文件中使用。
static for functions has the exact same impact as denoting a global variable as static ... the static function IS ONLY VISIBLE IN THE CURRENT FILE ...
函数的静态影响与将全局变量表示为静态的影响完全相同。静态函数仅在当前文件中可见。
Thus main can NEVER be static, because it would not be able to serve as the primary entry point for the program.
因此main不能是静态的,因为它不能作为程序的主要入口点。