Subject: SDL/OpenGL program fails to link when I want to use wgl-function
From: Eric Lilja <[email-masked]>
To: [email-masked]
Cc: [email-masked]
Date: Sun, 22 Oct 2006 22:23:46 +0200

Hello, I'm using a fully updated cygwin and mingw developement binaries 
of SDL (version 1.2.11). Consider the following program:

//#include <windows.h>
#include <SDL.h>
#include <GL/gl.h>

#include <cassert>

static void
display()
{
    glClear(GL_COLOR_BUFFER_BIT);
	
    glBegin(GL_POLYGON);
    glVertex2f(-0.5f,-0.5f);
    glVertex2f(-0.5f, 0.5f);
    glVertex2f( 0.5f, 0.5f);
    glVertex2f( 0.5f,-0.5f);
    glEnd();
	
    SDL_GL_SwapBuffers();
}

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

    int retval = SDL_Init(SDL_INIT_VIDEO);

    assert(retval == 0);

    SDL_Surface *surface = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);

    assert(surface != NULL);

    SDL_WM_SetCaption("Simple SDL/OpenGL demo", NULL);

    SDL_Event event;

    //wglGetProcAddress("foo");

    while (true)
    {
       while (SDL_PollEvent(&event))
       {
          switch (event.type)
          {
             case SDL_QUIT:
             {
                SDL_Quit();

                exit(EXIT_SUCCESS);
             }
          }
       }

       display();
    }

    assert(0); /* Not reached. */
}

Makefile I use:
CXX = g++
CXXFLAGS = -Wall -Wextra -std=c++98 -pedantic -g -O0 -I 
~/SDL-1.2.11/include -c
LDFLAGS = -L ~/SDL-1.2.11/lib -lSDL -lopengl32 -o $(EXEC)
EXEC = 1-1.exe
OBJECTS = simple_1-1.o

all: $(OBJECTS)
	$(CXX) $^ $(LDFLAGS)

%.o: %.cpp
	$(CXX) $(CXXFLAGS) $<

clean:
	rm -f $(OBJECTS) $(EXEC) *~ *.stackdump

This compiles, links, and runs just fine. However, now I want to use one 
of the wgl-functions (namely wglGetProcAddress). For that I have to 
include <windows.h>. The program compiles, but the linker fails with:
$ make
g++ -Wall -Wextra -std=c++98 -pedantic -g -O0 -I ~/SDL-1.2.11/include -c 
simple_1-1.cpp
g++ simple_1-1.o -L ~/SDL-1.2.11/lib -lSDL -lopengl32 -o 1-1.exe
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../libcygwin.a(libcmain.o):(.text+0xab): 
undefined reference to `_WinMain@16'

I tried adding -lSDLmain and -mwindows to the linker options, but it 
didn't help. Still the same linking error. Posting this to both the 
cygwin and sdl mailing list, hope that it doesn't offend anyone. 
Attaching the working program and Makefile. What must I do to get it to 
work? Works just fine under MSVC++ but I'd much rather use cygwin/gcc.

/ E
#include <SDL.h>
#include <GL/gl.h>

#include <cassert>

static void
display()
{
   glClear(GL_COLOR_BUFFER_BIT);
	
   glBegin(GL_POLYGON);
   glVertex2f(-0.5f,-0.5f);
   glVertex2f(-0.5f, 0.5f);
   glVertex2f( 0.5f, 0.5f);
   glVertex2f( 0.5f,-0.5f);
   glEnd();
	
   SDL_GL_SwapBuffers();
}

int
main(int argc, char *argv[])
{
   (void)argc;
   (void)argv;
   
   int retval = SDL_Init(SDL_INIT_VIDEO);

   assert(retval == 0);

   SDL_Surface *surface = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);

   assert(surface != NULL);

   SDL_WM_SetCaption("Simple SDL/OpenGL demo", NULL);

   SDL_Event event;

   while (true)
   {
      while (SDL_PollEvent(&event))
      {
         switch (event.type)
         {
            case SDL_QUIT:
            {
               SDL_Quit();
               
               exit(EXIT_SUCCESS);
            }
         }
      }

      display();
   }

   assert(0); /* Not reached. */
}
CXX = g++
CXXFLAGS = -Wall -Wextra -std=c++98 -pedantic -g -O0 -I ~/SDL-1.2.11/include -c
LDFLAGS = -L ~/SDL-1.2.11/lib -lSDL -lopengl32 -o $(EXEC)
EXEC = 1-1.exe
OBJECTS = simple_1-1.o

all: $(OBJECTS)
	$(CXX) $^ $(LDFLAGS)

%.o: %.cpp
	$(CXX) $(CXXFLAGS) $<

clean:
	rm -f $(OBJECTS) $(EXEC) *~ *.stackdump

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/