2

I am currently taking the cs50 Harvard course via EDX and am working on the Cash/Greedy Algorithm problem from PSET1/Week1. I have it all written out and I keep getting this error;

~/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)

Here is my code.

#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);
 }

Can someone help me figure this out without breaking the Harvard honor code?

Acorn
  • 24,970
  • 5
  • 40
  • 69
Josh Gray
  • 69
  • 1
  • 1
  • 5
  • 3
    See [Compilation error on function round](https://cs50.stackexchange.com/questions/21058/compilation-error-on-function-round-in-greedy-c-pset1) (tl;dr [link with -lm](https://manual.cs50.io/3/round)). – dxiv Sep 21 '20 at 06:25
  • 1
    This exercise has been posted so many times in StackOverflow that solving it without breaking the Harvard honor code is like trying not to get spoiled about who is Luke Skywalker's father in 2020. – Acorn Sep 21 '20 at 07:57
  • 1
    Why are you executing "clang" instead of "make"? – DinoCoderSaurus Sep 21 '20 at 11:23

2 Answers2

1

Use make cash. It will link both libraries (math and cs50).

In your compiling command, you link only cs50 library with -lcs50. Round function comes within library math. That's why compiler fails in finding reference to round.

There are some good sources to understand why undefined reference to occurs.

Enis Arik
  • 665
  • 10
  • 24
0

You are not linking the math library in your clang command either add a -lmath or use the make command.