我目前正在通过EDX学习cs50哈佛课程,并且正在研究PSET1/Week1 1的现金/贪婪算法问题。我已经把它都写出来了,但我一直收到这个错误;
~/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);
}有没有人能在不违反哈佛荣誉准则的情况下帮我解决这个问题?
发布于 2020-09-21 23:03:40
使用make cash。它将链接两个库(math和cs50)。
在编译命令中,您只需使用-lcs50链接cs50库。库math中包含Round函数。这就是为什么编译器找不到对round的引用。
有一些很好的sources可以用来理解为什么会发生undefined reference to。
https://stackoverflow.com/questions/63987047
复制相似问题