Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this small function(drawing a circle in opengl) compile in c?

Tags:

c

math

opengl

I'm doing some experiments with opengl in c for linux. I've got the following function that would draw a circle given those parameters. I've included

 #include <stdlib.h>
 #include <math.h>
 #include <GL/gl.h>
 #include <GL/glut.h>

However when I compile:

gcc fiver.c -o fiver -lglut

I get:

   /usr/bin/ld: /tmp/ccGdx4hW.o: undefined reference to symbol 'sin@@GLIBC_2.2.5'
   /usr/bin/ld: note: 'sin@@GLIBC_2.2.5' is defined in DSO /lib64/libm.so.6 so try  
   adding it to the linker command line
  /lib64/libm.so.6: could not read symbols: Invalid operation
   collect2: ld returned 1 exit status

The function is the following:

void drawCircle (int xc, int yc, int rad) {
//
// draw a circle centered at (xc,yc) with radius rad
//
  glBegin(GL_LINE_LOOP);
//
  int angle;
  for(angle = 0; angle < 365; angle = angle+5) {
    double angle_radians = angle * (float)3.14159 / (float)180;
    float x = xc + rad * (float)cos(angle_radians);
    float y = yc + rad * (float)sin(angle_radians);
    glVertex3f(x,0,y);
  }

  glEnd();
}

Does anyone know what's wrong?

like image 439
dasen Avatar asked Dec 31 '25 08:12

dasen


1 Answers

The linker cannot find the definition of sin() function. You need to link your application against the math library. Compile with:

gcc fiver.c -o fiver -lglut -lm
like image 103
peoro Avatar answered Jan 02 '26 00:01

peoro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!