SDL2 game immediately closes on Windows 10 - compiled using cl command and batch file


After watching some Handmade Hero I thought I'd try getting used to Windows command prompt. I've been working on a 2D game in C that was originally being developed on MacOS and Linux. It also worked on Windows but you needed MinGW installed.

I decided to switch to MSVC and batch files to build my game, and it's been a struggle getting anything working.

Here is the Batch file:

@echo off

mkdir build\
pushd build\
cl /W4 /FC /Zi ^
/I..\sdl2\include /I..\..\include /I..\..\include\adt /Isdl2\include ^
..\..\src\adt\*.c ..\..\src\*.c ^
/link ..\sdl2\lib\SDL2.lib ..\sdl2\lib\SDL2main.lib ..\sdl2\lib\SDL2_image.lib ^
/o:game.exe ^

Using MSVC on developer command prompt was a bit of a challenge. The syntax is different than GCC, and wasn't sure about some of the commands/arguments. But the idea is to compile, then link into a game.exe executable.

Folder Structure

    (output files from MacOS/Linux)
    *.h (other header files related to game)
    *.c (other source files related to game)
        (where the .exe is placed)

Example Header Usage

#ifndef GRAPHICS_H
#define GRAPHICS_H

#include "HashTableAPI.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>

#ifdef __linux__
    #include <SDL2/SDL.h>
    #include <SDL2/SDL_image.h>
#elif _WIN32
    #include "SDL.h"
    #include "SDL_image.h" // Complains this file is missing, even though it's in the same folder as SDL.h

The First Issue

The first C file compiled is HashTableAPI.c. There is always an executable created named HashTableAPI.exe, even if I use the /o:game.exe argument.

The Second (main) Issue

My program compiles with no warnings (except all the GCC function warnings), but the program crashes immediately after starting. The only valuable pieces of info I get from the output are:

'HashTableAPI.exe' (Win32): Loaded 'C:\Users\17058\Desktop\git-repos\Dog-Gone-Wild\windows\build\SDL2_image.dll'. Module was built without symbols

'HashTableAPI.exe' (Win32): Loaded 'C:\Users\17058\Desktop\git-repos\Dog-Gone-Wild\windows\build\SDL2.dll'. Module was built without symbols.

The program '[12984] HashTableAPI.exe' has exited with code -1 (0xffffffff).

The Questions

  • Does anyone have insight why it's not working? Clearly I am doing something wrong

  • Am I missing a WinMain() function? I have an int main(int argc, char*[] argv).

  • How should I organize my project structure if I will be supporting multiple operating systems? I feel a little confused what should go where.

Thank you so much for your time!

asked on Stack Overflow Jan 30, 2020 by AdamInTheOculus • edited Jan 30, 2020 by AdamInTheOculus

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0