c ++ linux编译错误:未定义引用`main'

时间:2010-11-21 04:37:42

标签: c++ compiler-construction

我已经查找了这个错误并尝试了一些解决方案,但没有找到任何东西,此时我只想编译它。

我得到的错误是:

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main'

使用:

g++ -lglut Solar.cpp

代码在这里:

using namespace std;
#include <stdio.h>
#include <GL/glut.h>
#include "math.h"

class Solar {

  int main(){
    initializeGL();
    //Stars Alpha = new Stars(5.0);
    //Stars *Alpha = new Stars(5.0);
    //Planets *Awe = new Planets(.6,2,30,"Awe",0.0,0.0,0.0);
    paintGL();
    return 0;
  }



  vid initializeGL(){
      glShadeModel(GL_SMOOTH);

      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      glClearDepth(1.0f);

      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_LEQUAL);

      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

      // lighting stuff
      GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
      GLfloat diffuse[] = {0.9, 0.9, 0.9, 1.0};
      GLfloat specular[] = {0.4, 0.4, 0.4, 1.0};
      GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
      glLightfv( GL_LIGHT0, GL_POSITION, position0 );
      glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
      glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
      glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
      GLfloat position1[] = {-1.0, -1.0, -1.0, 0.0};
      glLightfv( GL_LIGHT1, GL_POSITION, position1 );
      glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
      glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse );
      glLightfv( GL_LIGHT1, GL_SPECULAR, specular );

      glEnable( GL_LIGHTING );
      glEnable( GL_LIGHT0 );
      glEnable( GL_LIGHT1 );
      glEnable( GL_COLOR_MATERIAL );

      /* Draws the Grid*/
      drawRGrid();
  }

  void resizeGL( int width, int height ){
    height = height?height:1;

    glViewport( 0, 0, (GLint)width, (GLint)height );

    // update projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,.10f,200.0f);

    // modeview matrix is simply identity
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
  }

  void paintGL(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //set camera position using gluLookAt
    glLoadIdentity();
    gluLookAt(0.0f,0.0f,0.0f,0.0f,0.0f,-200.0f,0.0f,1.0f,0.0f);

  }


  void doCircle(double x, double y, double radius){
    glEnable(GL_BLEND);
    double y1=y+radius;
    double x1=x;
    glBegin(GL_LINE_STRIP);
    for(double angle=0.0f;angle<=(2.0f*3.14159);angle+=0.01f){
      double x2=x+(radius*(float)sin((double)angle));
      double y2=y+(radius*(float)cos((double)angle));
      glColor3f(1.0,1.0,1.0); //White
      glVertex2d(x1,y1);
      y1=y2;
      x1=x2;
    }
    glEnd();
    glDisable(GL_BLEND);
  }

  void drawRGrid(){
    float xCirc = 0;
    float yCirc = 0;
    int numCircles = 5;
    int threesixty = 360;
    int numLines = 20;

    //draws my circles
    for (int i=0; i < numCircles;i++ ){
      doCircle(1.0,1.0,i);
    }

    //draws my lines
    for (int j=0; j < threesixty / numLines;j+= numLines){
      // multiply by numCircles to get to extend to 
      // that length
      drawLines(sin(j)*numCircles,sin(j)*numCircles);
    }

  }

  void drawLines(float xCirc, float yCirc){
    glBegin(GL_LINES);
    glVertex2f(0,0);
    glVertex2f(xCirc,yCirc);
    glEnd();
  }



};

任何帮助都将非常感谢!

3 个答案:

答案 0 :(得分:6)

您已将main()声明为成员函数。

应用程序启动时调用的main()函数需要是全局命名空间中的非成员函数。

good introductory C++ book可以解释这一点。

答案 1 :(得分:3)

您在课程main中声明了Solarmain应该是一个自由函数,即不包含在类中。

答案 2 :(得分:1)

无法找到

main,因为您编写的函数名为Solar::main(事实上,它有一个更有趣的名字)。您需要将其移至class Solar以下。然后,您可能希望将class Solar更改为struct Solar,直到引入成员变量为止。最后,您可能希望将main重写为

extern "C" int main (int /* argc */, char *const * /* argv */) {
    Solar solar;
    solar.initializeGL();
    //Stars Alpha = new Stars(5.0);
    //Stars *Alpha = new Stars(5.0);
    //Planets *Awe = new Planets(.6,2,30,"Awe",0.0,0.0,0.0);
    solar.paintGL();
    return 0;
}