简体   繁体   English

无法编译来自 The C Programming Language 的示例程序

[英]Can't compile an example program from The C Programming Language

I know programming in Java, but I'm reading "The C Programming Language", second edition.我知道 Java 编程,但我正在阅读“C 编程语言”,第二版。 It says that to compile and run a program from the command line you have to do:它说要从命令行编译和运行程序,您必须执行以下操作:

$ cc hello.c  //assuming the name of the file is hello.c
./a.out

and hello world is printed.并打印hello world I tried that and worked fine.我试过了,效果很好。


Now, there is another example that prints a bunch of Celsius and Fahrenheit temperatures, so I created a file called TemperatureConverter.c with the given code.现在,还有另一个示例可以打印一堆摄氏和华氏温度,因此我使用给定的代码创建了一个名为TemperatureConverter.c的文件。 However, in the command line, when I ran:但是,在命令行中,当我运行时:

$ cc TemperatureConverter.c

I got an error:我有一个错误:

/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../lib64/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../lib64/crt1.o: 在函数`_start': (.text+0x20): 未定义的引用到“主要”
collect2: error: ld returned 1 exit status collect2: 错误: ld 返回 1 个退出状态

I don't know what this means.我不知道这是什么意思。 I tried double checking my syntax and it looks correct.我尝试仔细检查我的语法,它看起来是正确的。 I included the screenshot of the given code compared to the code I wrote, in case anybody notices a difference that may be causing the error:我将给定代码的屏幕截图与我编写的代码进行了比较,以防有人注意到可能导致错误的差异:

给定代码的屏幕截图

#include <stdio.h>

main()
{
  float fahr, celcius;
  int lower, upper, step;

  lower = 0;
  upper = 300;
  step = 20;

  fahr = lower;
  while (fahr <= upper) {
    celcius = (5.0/9.0) * (fahr - 32.0);
    printf("%3.0f %6.1f\n", fahr, celcius);
    fahr = fahr + step;
  }
}

I just ran your code in cygwin using the same command and it compiled/ran fine.我只是使用相同的命令在 cygwin 中运行您的代码,并且它编译/运行良好。 I will say the same things others have mentioned.我会说其他人提到的同样的事情。

  1. main has the return type int. main 的返回类型为 int。
  2. it's parameters should be (void) or (int argc, **int argv).它的参数应该是 (void) 或 (int argc, **int argv)。 You can have main() however this lets any parameters be passed into the main function.您可以使用 main() 但是这允许将任何参数传递给 main 函数。
  3. Third the main function should end with return 0;第三个 main 函数应该以 return 0 结束; This indicates that the program exited correctly.这表明程序正确退出。

In the end it should look like this最后它应该是这样的

#include <stdio.h>

int main(void)
{
  float fahr, celcius;
  int lower, upper, step;

  lower = 0;
  upper = 300;
  step = 20;

  fahr = lower;
  while (fahr <= upper) {
    celcius = (5.0/9.0) * (fahr - 32.0);
    printf("%3.0f %6.1f\n", fahr, celcius);
    fahr = fahr + step;
  }
  return 0;
}

There's absolutely nothing syntacticly wrong with your code.您的代码在语法上绝对没有错误。 Though the modifications suggested by FrozenHawk are a good idea.尽管 FrozenHawk 建议的修改是一个好主意。 I noticed the linker error talks about an undefined reference to main in function _start .我注意到链接器错误谈到了函数_startmain的未定义引用。 I've never even heard of a function _start as the starting point in ac file;我什至从未听说过函数_start作为 ac 文件的起点; though that could be an internal, compiler specific, thing not strictly specified in the standards.虽然这可能是一个内部的、特定于编译器的东西,但标准中没有严格规定。

You should be absolutely sure you're trying to compile the correct file.您应该绝对确定您正在尝试编译正确的文件。 Use cat on the same argument for extra assurance.在同一个参数上使用cat以获得额外的保证。 Your code compiled just fine for me, as is, and with the commands you used on Debian.你的代码对我来说编译得很好,就像你在 Debian 上使用的命令一样。 It seems unlikely, but this could be an issue with gcc.这似乎不太可能,但这可能是 gcc 的问题。 On my machine, no function called _start appears in any files in /usr/include/ , which implies something lower level.在我的机器上, /usr/include/中的任何文件中都没有出现名为_start ,这意味着较低级别的内容。 You might consider uninstalling and reinstalling as a last resort.您可能会考虑将卸载和重新安装作为最后的手段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM