首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CS50 -编译问题

CS50 -编译问题
EN

Stack Overflow用户
提问于 2020-09-21 14:15:37
回答 1查看 166关注 0票数 2

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

代码语言:javascript
复制
~/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)

这是我的代码。

代码语言:javascript
复制
#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);
 }

有没有人能在不违反哈佛荣誉准则的情况下帮我解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2020-09-21 23:03:40

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

在编译命令中,您只需使用-lcs50链接cs50库。库math中包含Round函数。这就是为什么编译器找不到对round的引用。

有一些很好的sources可以用来理解为什么会发生undefined reference to

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63987047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档