SDL on Cygwin: Undefined reference to 'WinMain@16'(SOLVED)

Started by
0 comments, last by SpookieDookie 9 years, 3 months ago

From my googling, a lot of people have been getting this error, but none of them solutions worked for me since I am using Cygwin. I have to learn the UNIX environment for my class, but I have a windows OS, so I am using Cygwin.

I followed LazyFoo's tutorials here:

Setting up using g++ tutorial:

http://lazyfoo.net/tutorials/SDL/01_hello_SDL/linux/index.php

Make file tutorial:

http://lazyfoo.net/tutorials/SDL/01_hello_SDL/linux/cli/index.php

The error:


$ make all
g++ 01_hello_SDL.cpp -w -lSDL2 -o 01_hello_SDL
/usr/lib/gcc/i686-pc-cygwin/4.8.3/../../../libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-1.7.33-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain@16'
collect2: error: ld returned 1 exit status
Makefile:23: recipe for target 'all' failed
make: *** [all] Error 1

The libcmain.c file(that the error above refers to):


/* libcmain.c

   Copyright 1996, 1997, 1998, 2000, 2001, 2004, 2006, 2009 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include <windows.h>

#define SP " \t\n"

/* Allow apps which don't have a main to work, as long as they define WinMain */
int
main ()
{
  HMODULE x = GetModuleHandle (0);
  char *s = GetCommandLine ();
  STARTUPINFO si;
  char *nexts;

  s += strspn (s, SP);

  if (*s != '"')
    nexts = strpbrk (s, SP);
  else
    while ((nexts = strchr (s + 1, '"')) != NULL && nexts[-1] == '\\')
      s = nexts;

  if (!nexts)
    nexts = strchr (s, '\0');
  else if (*++nexts)
    nexts += strspn (nexts, SP);

  GetStartupInfo (&si);

  return WinMain (x, 0, nexts,
          ((si.dwFlags & STARTF_USESHOWWINDOW) != 0
           ? si.wShowWindow
           : SW_SHOWNORMAL));
}

Make file:


#Copyright Notice:
#The files within this zip file are copyrighted by Lazy Foo' Productions (2004-2014)
#and may not be redistributed without written permission.

#OBJS specifies which files to compile as part of the project
OBJS = 01_hello_SDL.cpp

#CC specifies which compiler we're using
CC = g++

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -w

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = 01_hello_SDL

#This is the target that compiles our executable
all : $(OBJS)
    $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)

Code:


/*This source code copyrighted by Lazy Foo' Productions (2004-2013)
and may not be redistributed without written permission.*/

//Using SDL and standard IO
#include<SDL2/SDL.h>
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;



int main( int argc, char* args[] )
{

    //The window we'll be rendering to
    SDL_Window* window = NULL;

    //The surface contained by the window
    SDL_Surface* screenSurface = NULL;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
    }

    else
    {

        //Create window
        window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );

        if( window == NULL )
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        }

        else
        {

            //Get window surface
            screenSurface = SDL_GetWindowSurface( window );



            //Fill the surface white
            SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
           
            //Update the surface
            SDL_UpdateWindowSurface( window );

            //Wait two seconds
            SDL_Delay( 2000 );

        }
    }

    //Destroy window
    SDL_DestroyWindow( window );

    //Quit SDL subsystems
    SDL_Quit();

    return 0;
}

Anyone know how to solve this error?

Advertisement

SOLUTION:

I solved the problem after running an SDL test file and seeing what Cygwin used to link with. So I changed my make file linkers accordingly to this(it works now):


#Copyright Notice:
#The files within this zip file are copyrighted by Lazy Foo' Productions (2004-2014)
#and may not be redistributed without written permission.

#OBJS specifies which files to compile as part of the project
OBJS = 01_hello_SDL.cpp

#CC specifies which compiler we're using
CC = g++

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -w

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -L/usr/local/lib 
LINKER_FLAGS1 = -lcygwin
LINKER_FLAGS2 = -lSDL2main
LINKER_FLAGS3 = -lSDL2
LINKER_FLAGS4 = -mwindows

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = 01_hello_SDL

#This is the target that compiles our executable
all : $(OBJS)

	$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) $(LINKER_FLAGS1) $(LINKER_FLAGS2) $(LINKER_FLAGS3) $(LINKER_FLAGS4) -o $(OBJ_NAME)

This topic is closed to new replies.

Advertisement