【发布时间】: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);
}
有人可以在不违反哈佛荣誉守则的情况下帮我解决这个问题吗?
【问题讨论】:
-
见 Compilation error on function round (tl;dr link with -lm)。
-
这个练习已经在 StackOverflow 上发布了很多次,以至于在不违反哈佛荣誉守则的情况下解决它就像试图不被宠坏谁是 2020 年卢克天行者的父亲一样。
-
你为什么执行“clang”而不是“make”?