为什么这个小函数(在opengl中画一个圆圈)不会在c中编译?

das*_*sen 2 c opengl math

我正在用linux中的opengl进行一些实验.我有以下功能,可以根据这些参数绘制一个圆圈.我已经包括在内了

 #include <stdlib.h>
 #include <math.h>
 #include <GL/gl.h>
 #include <GL/glut.h>
Run Code Online (Sandbox Code Playgroud)

但是当我编译时:

gcc fiver.c -o fiver -lglut
Run Code Online (Sandbox Code Playgroud)

我明白了:

   /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
Run Code Online (Sandbox Code Playgroud)

功能如下:

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();
}
Run Code Online (Sandbox Code Playgroud)

有谁知道什么是错的?

peo*_*oro 17

链接器找不到sin()函数的定义.您需要将应用程序与数学库链接.编译:

gcc fiver.c -o fiver -lglut -lm
Run Code Online (Sandbox Code Playgroud)