【问题标题】:Compiler errors: undefined reference to `print' [closed]编译器错误:未定义对“打印”的引用 [关闭]
【发布时间】:2015-07-10 07:34:08
【问题描述】:

在我的代码中,我在尝试编译时不断收到这些错误

/tmp/ccshIakV.o:在函数`main'中:

project2.c:(.text+0x370): undefined reference to `print'

collect2: 错误:ld 返回 1 个退出状态

这是我的代码

int main(void)
{
double a = 0;
double b = 0;
double c = 0;
double d = 0;
double e = 0;
double f = 0;
double g = 0;
double h = 0;
double i = 0;

char command = '\0';

printf("\n      Welcome\n");
printf("     Aquapodz Stress Analysis Program\n");
printf("    ==================================\n");
while (command != 'x');
    {
printf("\n\n(a), (b), or (c), enter trial data for vendor.\n(f)ail-rate,     (m)ean stress, (s)ummary, e(x)it\n");
printf("Please enter a command");
scanf("%c", &command);

if (command == 'a')
{
printf("Please enter stress values (GPa) for this trial.");
scanf("%lf", &a);
scanf("%lf", &b);
scanf("%lf", &c);
}
else if (command == 'b')
{
printf("Please enter stress values (GPa) for this trial.");
scanf("%lf", &d);
scanf("%lf", &e);
scanf("%lf", &f);
}
else if (command == 'c')
{
printf("Please enter stress values (GPa) for this trial.");
scanf("%lf", &g);
scanf("%lf", &h);
scanf("%lf", &i);
}
else if (command == 'f')
{

printf("Average failure rate:\nAzuview:%f\nBublon:%f\nCryztal:%f\n",        a+b+c, d+e+f, g+h+i);
}
else if (command == 'm')
{
printf("Average mean stress:\nAzuview:%f\nBublon:%f\nCryztal:%f\n",     a+b+c/3, d+e+f/3, g+h+i/3);
}
else if (command == 's')
{
print("Total (pass / fail) so far:\nAzuview:%f(%f/0)\nBublon:%f(%f/0)    \nCryztal:%f(%f/0)\n", a+b+c, a+b+c, d+e+f, d+e+f, g+h+i, g+h+i);
}
else if (command == 'x')
{

}
else
{
printf("Invalid Command! Please Try Again :)");
}

}
printf("Goodbye, Please Come Again!");
  return 0;
}

我以前从未遇到过这些错误。

【问题讨论】:

  • 你已经在你的实际代码中包含了头文件?
  • 一般来说,“未定义的引用”错误消息通常意味着您正在使用没有定义的函数或变量。例如,您包含一个声明函数 foo 的头文件,但在编译时忘记链接实现 foo 的源/目标文件。
  • "未定义的对print的引用" - 那么,您将print 更改为printf 怎么样?编译器再明显不过了!!!
  • 是的,我只是忘记在帖子中添加它
  • @barakmanos 编译器在这里根本不是“显而易见的”,而是在没有适当警告设置的情况下完全错过了错误。隐式函数声明是一个指示有问题的,但它们是允许的。这里的错误信息来自链接器,所以甚至没有行号。 -> 使用编译器警告,它们会很明显。

标签: c compiler-errors


【解决方案1】:
else if (command == 's')
{
    print("Total (pass / fail) so far:\nAzuview:%f(%f/0)\nBublon:%f(%f/0)    \nCryztal:%f(%f/0)\n", a+b+c, a+b+c, d+e+f, d+e+f, g+h+i, g+h+i);
}

请仔细看看这部分。为避免这种情况,请使用编译器警告(例如,-Wall -Wextra 用于 gcc)——在这种情况下,它们会警告隐式函数声明。

附带说明,关于许多其他行:如果没有要格式化的数据,则不需要printf()。请改用puts()(如果不应该有换行符,则使用fputs()stdout)。

【讨论】:

  • 这是最好的答案。您总是需要首先使用gcc -Wall -Wextra -g 进行编译(询问所有警告、额外警告和调试信息)。
【解决方案2】:

您的代码中有错字。您错过了printf() 通话中的f

你必须改变

else if (command == 's')
{
print("Total (pass / fail) so far:\nAzuview:%f(%f/0)\nBublon:%f(%f/0)    \nCryztal:%f(%f/0)\n", a+b+c, a+b+c, d+e+f, d+e+f, g+h+i, g+h+i);
}

else if (command == 's')
{
printf("Total (pass / fail) so far:\nAzuview:%f(%f/0)\nBublon:%f(%f/0)    \nCryztal:%f(%f/0)\n", a+b+c, a+b+c, d+e+f, d+e+f, g+h+i, g+h+i);
     ^
}

【讨论】:

  • 谢谢你,我不知道我怎么会这么笨!!!
  • @ThePixelPerson 不客气。 :-)(请不要大喊
  • 另一个快速的问题,当我运行 y 程序时,它会询问一个命令,如果你选择 a、b 或 c,系统会提示你给它 3 个值。我的问题是,当我输入任何命令时,它都会运行,我可以给它们赋值,但是我的无效命令消息会弹出,即使进程运行良好并且是有效命令。
【解决方案3】:

在这里,您需要使用printf 而不仅仅是print

else if (command == 's')
{
print("Total (pass / fail) so far:\nAzuview:%f(%f/0)\nBublon:%f(%f/0)    \nCryztal:%f(%f/0)\n", a+b+c, a+b+c, d+e+f, d+e+f, g+h+i, g+h+i);
}

【讨论】:

    【解决方案4】:
     else if (command == 's')
     {
    
        print("Total (pass / fail) so far:\nAzuview:%f(%f/0)\nBublon:%f(%f/0)        \nCryztal:%f(%f/0)\n", a+b+c, a+b+c, d+e+f, d+e+f, g+h+i, g+h+i);
     }
    

    在这一行中,您输入了print 而不是printf。请指正

    【讨论】:

      【解决方案5】:

      你错过了printf的“f”

      else if (command == 's')
      {
          print("Total (pass / fail) so far:\nAzuview:%f(%f/0)\nBublon:%f(%f/0) \nCryztal:%f(%f/0)\n", a+b+c, a+b+c, d+e+f, d+e+f, g+h+i, g+h+i);
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 1970-01-01
        • 2013-02-02
        • 1970-01-01
        相关资源
        最近更新 更多