Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
Every time I try to compile this code I get this error:
 
undefined reference to `cos'
collect2: ld returned 1 exit status
 
I don't understand what's wrong. Can someone please explain why keep getting this error even though I have >?
 
#include <stdio.h>
#include <math.h>

int main (){
  double range, value;
  int count,offset, sp;
 
  printf("For loop example - simple cosine graph\n\n");
 
  range = 0.0;
  for(count = 1; range <= 40; count++){
 
    value = cos (range); //caldulate value

//convert decimal value into interger to use
//as number of blanks to display

offset = (value + 1.0) * 15.0;
 
//display the indicated number of blanks

for (sp=0; sp < offset; sp++)
printf(" ");
 
//display the mrker for the graph

printf("*\n");
 
range += 0.5;
}
 
return 0;
 
}
Posted 29-Apr-13 11:26am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should link with the math library, e.g.
gcc myapp.c -o myapp -lm
The documentation[^] states: "Link with -lm".
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100