Why won't this small function(drawing a circle in opengl) compile in c?
I'm doing开发者_JAVA技巧 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?
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
精彩评论