CS50 - Compiling Issues

Viewed 399

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?

2 Answers

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.

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

Related