csdl-2sdl-image

SDL application exits without entering main - SDL Image


I have an application that runs fine on Ubuntu but immediately exit at startup, WITHOUT ANY ERRORS, on Windows.

It seems that main() function is not entered.

Application has been compiled without errors and it uses SDL_image.h. When (in the same application) the code that uses SDL Image was not present the application was running fine.

In the .exe directory there are all the needed dlls, SDL2.dll, libjpeg-9.dll, libpng16-16.dll, libtiff-5.dll, libwebp-7.dll, SDL2_image.dll and zlib1.dll.

IMG_Init() is called before any other image functions in this way:

(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG)

and, as I said before, tha application runs without problems on Ubuntu.

This is a reproducible example.

This code works:

makefile

CC = gcc -fno-pie -no-pie -m32
CFLAGS = -IC:/sdl2/include -O0 -ggdb
LIBS = -LC:/sdl2/lib/x86 -lSDL2main -lSDL2

all:
    $(CC) $(CFLAGS) main.c $(LIBS) -o application.exe

main.c

#include <stdio.h>
#include "SDL.h"


int main(int argc, char **argv)
{
    if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
    {
        SDL_Log("SDL fails to initialize video subsystem!\n%s", SDL_GetError());
        return -1;
    }
    else
        printf("SDL Video was initialized fine!\n");

    return 0;
}

This code, where I use SDL Image library, doesn't do nothing, no errors, immediately exits:

makefile

CC = gcc -fno-pie -no-pie -m32
CFLAGS = -IC:/sdl2/include -IC:/sdl2_image/include -O0 -ggdb
LIBS = -LC:/sdl2/lib/x86 -LC:/sdl2_image/lib/x86 -lSDL2main -lSDL2 -lSDL2_image

all:
    $(CC) $(CFLAGS) main.c $(LIBS) -o application.exe

main.c

#include <stdio.h>
#include "SDL.h"
#include "SDL_image.h"


int main(int argc, char **argv)
{
    if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
    {
        SDL_Log("SDL fails to initialize video subsystem!\n%s", SDL_GetError());
        return -1;
    }
    else
        printf("SDL Video was initialized fine!\n");

    if((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG)
    {
        SDL_Log("SDL fails to initialize image handler!\n%s", IMG_GetError());
        return -1;
    }
    else
        printf("SDL Image was initialized fine!\n");

    return 0;
}

As I said before, it seems that the problem is the windows library (or the environment) and this is why I didn't add a reproducible code before


Solution

  • After 2 days I found a solution.

    First of all (but it isn't my problem) the SDL Image library is not so "smart" so it is important that SDL2_image.dll library and the used ones from libjpeg-9.dll, libpng16-16.dll, libtiff-5.dll, libwebp-7.dll and zlib1.dll are present. You'll not receive any error if one of needed library is missing.

    I had a problem in my environment, on this machine I use Visual Studio and Eclipse. I made a mistake and my makefile pointed to Visual Studio SDL libraries and not to MinGW ones.

    I don't know why but the first reproducible example works fine also with this mistake (and without -lmingw32), so it pointed me out of the correct way.

    I did this change on makefile and now it works:

    CFLAGS = -IC:/sdl2_mingw/i686-w64-mingw32/include/SDL2 -IC:/sdl2_image_mingw/i686-w64-mingw32/include/SDL2 -O0 -ggdb
    LIBS = -lmingw32 -LC:/sdl2_mingw/i686-w64-mingw32/lib -LC:/sdl2_image_mingw/i686-w64-mingw32/lib -lSDL2main -lSDL2 -lSDL2_image