line 6: syntax error near unexpected token `('line 6: `int main()'

Started by
2 comments, last by Bregma 5 years, 1 month ago

I'm trying to get into SDL1 and opengl, this compiles fine under gcc, -lSDL -lGL

why do I get error:

line 6: syntax error near unexpected token `('line 6: `int main()'

#include<SDL/SDL.h>
#include<GL/gl.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{

/*Inintialize SDL as usual */
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Error: %s\n", SDL_GetError());
return 1;
}

atexit(SDL_Quit);

/* Enable OpenGL double buffering */
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

/*Set the colour depth (16 bit 565). */
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);

/* Create a 640 x 480, 16 bit window with support for
Open GL rendering. Unfortunately we won't know whether
this is hardware accelerated. */
if (SDL_SetVideoMode(640, 480, 16, SDL_OPENGL) == NULL) {
printf("Error: %s\n", SDL_GetError());
return 1;
}

SDL_WM_SetCaption("OpenGL with SDL!", "OpenGL");

/* We can now use any OpenGL rendering commands */

glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glBegin(GL_POLYGON);
    glVertex2f(-0.5,-0.5);
    glVertex2f(-0.5,0.5);
    glVertex2f(0.5,0.5);
    glVertex2f(0.5,-0.5);
glEnd();
glFlush();
    

/*Display the back buffer to the screen */
SDL_GL_SwapBuffers();

/*Wait a few seconds. */
SDL_Delay(5000);

return 0;

}

 

Advertisement

Its fixed with:

  • int main(int argc, char *argv[])
  • {
  • (void)(argc);
  • (void)(argv);

Just so you know the reason, SDL.h (through a secondary include file) #defines a macro "main" that replaces your "main" with one of its own functions, and if you don't use the text replacement just right, you get that error.

Of course, #defining a macro with the same token as a reserved word in C++ is a violation of the standard and any program that uses SDL is malformed out of the gate an is invoking undefined behaviour.  Probably worth it to use SDL, but beware of gotchas like this.  The SDL docs do say you have to provide the argc/argv signature for main, and they're not kidding.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement