gpt4 book ai didi

c++ - 为什么静态 main() 类型的程序显示错误?

转载 作者:行者123 更新时间:2023-11-30 21:32:15 25 4
gpt4 key购买 nike

#include<stdio.h>
static int main()
{
printf("foo");
return 0;
}

代码出现错误

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

错误的原因是什么

最佳答案

在 C 中,static 是“隐藏”实现细节的主要方式。在 C 中将函数或变量标记为静态意味着将其可见性限制在定义它的翻译单元中。本质上,只有同一 C 文件内的函数才能引用它们。其他文件或库中的函数无法访问它们。

由于函数 main 需要从环境的启动代码(一段“引导”程序执行的代码)访问,隐藏它会使您的程序不可链接:编译器尝试查找main,但它是隐藏的,因此链接器会发出错误。

关于c++ - 为什么静态 main() 类型的程序显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28659917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com