Click here to Skip to main content
13,091,145 members (49,644 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 <math.h>>?

#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
range += 0.5;
return 0;
Posted 29-Apr-13 10:26am

1 solution

Rate this: bad
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".

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2017
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