My SDL2 programs crash strangely

I’m trying to get the SDL2 library to work in C++ with Code::Blocks. After testing the installation (fighting the linker and debugger again and again…;), I managed to run the following code without fatal errors:

#include <SDL2/SDL.h>

int main(int argc, char *argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0) return 1;

    SDL_Quit();

    return 0;
}

But at the end, the program returns the integer 32762 with this sentence: "Process return 32762 (0x7FFA) " and I don’t understand why it doesn’t just return 0 or 1. Even if I don’t have errors or warnings, the program does not operate normally. Honestly, I’m racking my brains about this problem, and Google hasn’t helped me much. However, I tried to remove the first line of my code, with the include and the program returns 0. It’s as if the SDL2 header or libs is bugging my code and crashing the program… I don’t know why MinGW can’t provide a good executable code under Windows with SDL2. I admit that any help is welcome, because you are my last chance ! Thank you very much for giving me time.

Not sure, but maybe it could help to use some compilation and linking flags.

Here’s an example:

#include <SDL2/SDL.h>
#include <SDL2/SDL_assert.h>

int main(void)
{
    int ok = SDL_Init(SDL_INIT_VIDEO);
    SDL_assert(ok == 0);
    SDL_Quit();
    return 0;
}

I compile the above with GCC on Debian 11 via

gcc -g -fsanitize=undefined -Wall -Wextra -Werror -std=c90 -pedantic -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wc++-compat -Wconversion -Wshadow -Wwrite-strings test.c -o my_app -lm -lpthread -Llib -lSDL2 -lSDL2main

Consider compiling it with

I’m a little unsure how you came up with that particular set of flags. (For example, I don’t think -lSDL2main does anything on a Linux system – it’s specifically for addressing an idiosyncrasy of Windows.)

Have you tried just using the flags from pkg-config and see if the behavior is any different? Try something like:

gcc -Wall -Wextra $(pkg-config --cflags sdl2) -c test.c
gcc  -o my_app test.o $(pkg-config --libs sdl2)

and see if anything changes.

1 Like

I tested that using a Makefile of mine.
I tend to use all these flags in my projects.
Sorry for the potential confusion, though.

go here
https://www.codeblocks.org/downloads/binaries/

download
codeblocks-20.03mingw-setup.exe

from
Sourceforge.net

download should happen automatically

install codeblocks

then go here

and download
SDL2-devel-2.24.0-mingw.zip

unzip and copy this folder
x86_64-w64-mingw32
over to C:\SDL2 or maybe C:\development or whatever

then open codeblocks, Create a New Project, SDL2 project

a wizard comes up – give your project a name and location

it will ask you where SDL2 is

enter the path where you saved the SDL folder
C:\SDL2\x86_64-w64-mingw32

codeblocks will create a basic demo sdl2 project for you
you can view the C file in the codeblocks editor under the Sources folder

however, the code is a little bit wrong
change
#include <SDL.h>
to
#include <SDL2/SDL.h>

then save the file

and for this one example project there is an extra step that is needed
from codeblocks menu choose Settings / Global variables …
then press the New button
enter sdl2
and press OK
then in the base box enter the path where you saved the SDL folder
C:\SDL2\x86_64-w64-mingw32
then Close

you are only doing that because this demo project copies the sdl2 dlls over to the Debug and Build folders when you build the project

you can see this by going to the codeblocks menu and
choosing Project / Build Options / (change from Debug to project name)
and look for the tab named
Pre/post build steps
and in the Post-build steps you’ll see this line
XCOPY $(#sdl2)\bin*.dll $(TARGET_OUTPUT_DIR) /D /Y

so AFTER the exe is successfully built the final step of the build process will be to copy the dlls
that’s why you had to set the global variable

if you want to see the linker settings
then change to the tab named
Linker settings

also look at the tab named
Search directories
and under that loo at the sub tabs
Compiler
and
Linker

now from the menu choose Build / Build and run
a console window will open (where you will see the output of any printf and SDL_Log statements)
and another window will open that will be the executable you just built

when you close the executable you’ll see
exit code 0
in the console window

but in codeblocks you’ll see some weird exit code –
that is not the exit code of your executable, it is the exit code of the codeblock process that is running your executable

the console window only shows up when you run the program inside codeblocks
it doesn’t show up when you go to the debug or release folder and run the exe directly

you may have to go to your codeblocks install folder and copy
libgcc_s_seh-1.dll
libstdc+±6.dll
libwinpthread-1.dll

over to the debug and release folder