为什么程序用静态main()类型显示错误? [英] why the program with static main() type shows error?
问题描述
#include< stdio.h>
static int main()
{
printf(foo);
return 0;
}
代码给出错误
nfo):relocation 12具有无效的符号索引13
/ usr / bin / ld:/ usr / lib / debug / usr / lib / x86_64-linux-gnu / crt1。 o(.debug_info):relocation 13具有无效的符号索引13
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):relocation 14具有无效的符号索引13
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):relocation 15具有无效的符号索引13
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):relocation 16具有无效的符号索引13
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):relocation 17具有无效的符号索引13
/ usr / bin / ld:/ usr / lib /debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):relocation 18具有无效的符号索引13
/ usr / bin / ld:/ usr / lib / debug / usr / lib / x86_64-linux-gnu / crt1.o(.debug_info):relocation 19具有无效的符号索引21
/ usr / bin / ld:/ usr / lib / debug / usr / lib / x86_64-linux-gnu / crt1 .o(.debug_line):relocation 0具有无效的符号索引2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1。 o:在函数`_start':
(.text + 0x20):未定义引用`main'
collect2:错误:ld返回1退出状态
$
在C, static
是隐藏实现细节的主要方式。在C中标记函数或变量 static
意味着将其可见性限制在其定义的翻译单元。基本上,只有同一个C文件中的函数可以引用它们。来自其他文件或库的函数无法访问它们。 由于函数 main
需要从环境的启动代码(一段代码bootstraps你的程序执行)隐藏它使你的程序不可链接:编译器试图找到 main
,但它被隐藏,因此链接器发出错误。
#include<stdio.h>
static int main()
{
printf("foo");
return 0;
}
the code gives error
nfo): relocation 12 has invalid symbol index 13 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2 /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: error: ld returned 1 exit status
what's the reason behind the error
In C, static
is the primary way of "hiding" implementation details. Marking a function or a variable static
in C means restricting its visibility to the translation unit in which it is defined. Essentially, only functions inside the same C file can refer to them. Functions from other files or libraries have no way of accessing them.
Since function main
needs to be accessed from your environment's start-up code (a piece of code that "bootstraps" your program execution) hiding it renders your program non-linkable: compiler tries to find main
, but it is hidden, so the linker issues an error.
这篇关于为什么程序用静态main()类型显示错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!