Multiple definition of 'main' in C?

I got this error in my CodeBlocks Editor while working on the OpenGL project in C. This is my very first time using this, and YES, I've already seen questions with the same title as mine, but unfortunately, they couldn't help me. I tried making every line of code again, but it does the same. Could someone please help? Thanks.

Oh, yes, here is the code:

#include<windows.h>
#include<GL/glu.h>
#include<GL/glut.h>

GLfloat Cx=0,Cy=0,Cz=3;

void MyInit()
{
    glClearColor(0,0,0,1);
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1,1,-1,1,2,10);
    glMatrixMode(GL_MODELVIEW);
}

void Square(GLfloat A[],GLfloat B[],GLfloat C[],GLfloat D[])
{
    glBegin(GL_POLYGON);
        glVertex3fv(A);
        glVertex3fv(B);
        glVertex3fv(C);
        glVertex3fv(D);
    glEnd();
}

void Cube(GLfloat V0[],GLfloat V1[],GLfloat V2[],GLfloat V3[],GLfloat V4[],GLfloat V5[],GLfloat V6[],GLfloat V7[])
{
    glColor3f(1,0,0);
    Square(V0,V1,V2,V3);
    glColor3f(0,1,0);
    Square(V4,V5,V6,V7);
    glColor3f(0,0,1);
    Square(V0,V4,V7,V3);
    glColor3f(1,1,0);
    Square(V1,V5,V6,V2);
    glColor3f(1,0,1);
    Square(V3,V2,V6,V7);
    glColor3f(0,1,1);
    Square(V0,V1,V5,V4);
}

void Draw()
{
    GLfloat V[8][3] =   {
                            {-0.5, 0.5, 0.5},
                            { 0.5, 0.5, 0.5},
                            { 0.5,-0.5, 0.5},
                            {-0.5,-0.5, 0.5},
                            {-0.5, 0.5,-0.5},
                            { 0.5, 0.5,-0.5},
                            { 0.5,-0.5,-0.5},
                            {-0.5,-0.5,-0.5}
                        };
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    gluLookAt(Cx,Cy,Cz,0,0,0,0,1,0);

    Cube(V[0],V[1],V[2],V[3],V[4],V[5],V[6],V[7]);

    glutSwapBuffers();
}

void Key(unsigned char ch,int x,int y)
{
    switch(ch)
    {
        case 'x' : Cx = Cx - 0.5;   break;
        case 'X' : Cx = Cx + 0.5;   break;

        case 'y' : Cy = Cy - 0.5;   break;
        case 'Y' : Cy = Cy + 0.5;   break;

        case 'z' : Cz = Cz - 0.5;   break;
        case 'Z' : Cz = Cz + 0.5;   break;
    }
    glutPostRedisplay();
}

int main(int argC,char *argV[])
{
    glutInit(&argC,argV);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Color Cube with Camera");
    MyInit();
    glutDisplayFunc(Draw);
    glutKeyboardFunc(Key);
    glutMainLoop();
    return(0);
}

Here is the full message as it shows in editor:

C:\Users\Saric\Desktop\C\OpenGL-Sample#1\sample.c|82|multiple definition of 'main'; obj\Debug\main.o:C:/Users/Saric/Desktop/C/OpenGL-Sample#1/main.cpp:140: first defined here

1 answer

  • answered 2022-01-19 17:13 Serge Ballesta

    On my MSVC installation, gl/glu.h contains a main function:

    int main(int argC,char *argV[])
    {
        glutInit(&argC,argV);
        glutInitWindowSize(600,600);
        glutInitWindowPosition(100,150);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow("Color Cube with Camera");
        MyInit();
        glutDisplayFunc(Draw);
        glutKeyboardFunc(Key);
        glutMainLoop();
        return(0);
    }
    

    I a not used to OpenGL programming, but as this contains a main function that calls a glutMainLoop function, I would assume that you are not supposed to provide a main function, but only provide handlers for events.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum