5

I hope someone can help me. I am working through CS50x and am working on Pset1 - greedy. I am getting the following error whenever I compile my code:

/tmp/greedy-46be96.o: In function `main':
greedy.c:(.text+0x95): undefined reference to `round'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Any help will be greatly appreciated. I apologise if the question is vague, I have tried to be in depth. I have used man round in the terminal, and have searched everywhere, trying different solutions, but nothing has worked.

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
    float owed;
    float change;
    float quarter = 0.25;
    float dime = 0.10;
    float nickel = 0.05;
    float penny = 0.01;

    do {
        printf("How much change is owed?: ");
        owed = GetFloat();

    } while(owed <= 0);

    change = round(owed * 100);
}

I am using this command to compile my code:

clang -o greedy greedy.c -lcs50
Rekovni
  • 6,319
  • 3
  • 39
  • 62
Phrike
  • 91
  • 1
  • 2
  • 7
  • 4
    I think you missed to link to math library (add `-lm` in linker option, at the end) – Mathieu Sep 13 '16 at 14:37
  • 3
    Link the math library. In GCC `-lm`. – s7amuser Sep 13 '16 at 14:37
  • 3
    Hint: This is not a compilation error but a linking error. Can you please paste the command you use to compile/link the code? – mroman Sep 13 '16 at 14:38
  • I agree with purplepsycho. To verify, would you please [edit your question](http://stackoverflow.com/posts/39472662/edit) to show the exact command line you are using to compile your code? By the way, welcome to the site! Check out the [tour](https://stackoverflow.com/tour) for more about asking questions that will attract quality answers. – cxw Sep 13 '16 at 14:38
  • I apologise if this is a duplicate. I am very new to programming and didn't know what to search for. Thank you for all of your replies :) – Phrike Sep 13 '16 at 14:42
  • Sorry to be a pain in the arse. What command am I adding "-lm" to? – Phrike Sep 13 '16 at 14:48

1 Answers1

8

The following should work when you compile:

clang -o greedy greedy.c -lcs50 -lm

This links the math library for the compiler.

Rekovni
  • 6,319
  • 3
  • 39
  • 62