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, G开发者_运维知识库L_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.
The main()
function called when the application starts up needs to be a non-member function in the global namespace.
A good introductory C++ book would explain this.
You declared main
in class Solar
. main
is supposed to be a free function, i.e. not contained within a class.
main
cannot be found because the function you wrote is called Solar::main
(in truth, it has a much funnier name). You need to move it below class Solar
. Then you probably want to change class Solar
into struct Solar
until you introduce member variables. At last, you may want to rewrite main
to be
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;
}
精彩评论