简体   繁体   English

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

[英]c++ linux compile error: undefined reference to `main'

I have looked for this error and tried a few of the solutions, but have not found anything, at this point I would just like to compile it. 我已经找了这个错误并尝试了一些解决方案,但没有找到任何东西,此时我只想编译它。

The error I am getting is: 我得到的错误是:

/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'

using: 使用:

g++ -lglut Solar.cpp

the code is here: 代码在这里:

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();
  }



};

Any help will be great appreciated! 任何帮助将非常感谢!

You have declared main() as a member function. 您已将main()声明为成员函数。

The main() function called when the application starts up needs to be a non-member function in the global namespace. 应用程序启动时调用的main()函数需要是全局命名空间中的非成员函数。

A good introductory C++ book would explain this. 一本好的C ++入门书将解释这一点。

You declared main in class Solar . 你在Solar宣布main main is supposed to be a free function, ie not contained within a class. main应该是一个自由函数,即不包含在类中。

main cannot be found because the function you wrote is called Solar::main (in truth, it has a much funnier name). main无法找到,因为你写的函数叫做Solar::main (事实上​​,它有一个更有趣的名字)。 You need to move it below class Solar . 你需要将它移到class Solar下面。 Then you probably want to change class Solar into struct Solar until you introduce member variables. 那么您可能希望将class Solar更改为struct Solar直到您引入成员变量。 At last, you may want to rewrite main to be 最后,您可能想要重写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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM