【问题标题】:CS50 - Compiling IssuesCS50 - 编译问题
【发布时间】:2020-09-21 06:15:37
【问题描述】:

我目前正在通过 EDX 参加 cs50 哈佛课程,并且正在研究 PSET1/Week1 的现金/贪婪算法问题。我把它都写出来了,我一直收到这个错误;

~/pset1/ $ clang -o cash cash.c -lcs50
/tmp/cash-4f9816.o: In function `main':
cash.c:(.text+0x44): undefined reference to `round'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)

这是我的代码。

#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
    {

float dollars;
do
{
    dollars = get_float("Change: ");
}
while (dollars <= 0);


int cents = round(dollars * 100);
int coins = 0;

while (cents >= 25)
{
    coins ++;
    cents -= 25;
}


while (cents >= 10)
{
    coins ++;
    cents -= 10;
}


while (cents >= 5)
{
    coins ++;
    cents -= 5;
}


while (cents >= 1)
{
    coins ++;
    cents -= 1;
}

printf("You Will Receive %i Coin(s)\n)", coins);
 }

有人可以在不违反哈佛荣誉守则的情况下帮我解决这个问题吗?

【问题讨论】:

  • 这个练习已经在 StackOverflow 上发布了很多次,以至于在不违反哈佛荣誉守则的情况下解决它就像试图不被宠坏谁是 2020 年卢克天行者的父亲一样。
  • 你为什么执行“clang”而不是“make”?

标签: c cs50


【解决方案1】:

使用make cash。它将链接两个库(mathcs50)。

在您的编译命令中,您仅将 cs50 库与 -lcs50 链接。 Round 函数来自库 math。这就是编译器无法找到对round 的引用的原因。

有一些很好的sources 可以理解为什么会出现undefined reference to

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 2021-12-23
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多