【问题标题】:undefined reference to main error in gcc on centos.未定义对 centos 上 gcc 中主要错误的引用。
【发布时间】:2011-10-16 11:06:34
【问题描述】:

我正在尝试使用以下命令运行带有 gcc 的 hello world 程序

gcc hello.c

但我收到以下错误。

/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

【问题讨论】:

  • 请在您的帖子中添加 hello.c 的文本

标签: gcc reference undefined main


【解决方案1】:

看来,您的hello.c 中没有定义main() 函数。正确的代码是这样的:

#include <stdio.h>

int main(int argc, char**argv)  /// <<==-- here is a correct definition
{
    printf("Whatever you want\n");
    return 0;
}

【讨论】:

  • 缺少return 且参数不是强制性的。
  • 当然,只是想提一下,实际上并不需要它们。特别是对于以Hello world 开头的初学者来说,有时他们可能会有点混乱:)无论如何,+1,因为我会写一样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2022-01-02
  • 2012-09-04
相关资源
最近更新 更多