Eran Stockdale
Eran Stockdale

Reputation: 1

Linking SDL_Image with CMake makes my program exit immediately after being run

So, I've setup SDL2 and SDL_Image with CMake (both on their respective SDL2 branches), but when I add IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG); the program just doesn't do anything when run. Like, it doesn't even crash, it just doesn't run. The same issue happens if I link SDL2 as a shared library.

I'm on Windows 11 and I'm using MinGW (mingw32-make) (downloaded today so the most recent version).

Here's my CMakeLists.txt:

## Setup
cmake_minimum_required(VERSION 3.27.9)
project(Pacman LANGUAGES CXX)

## SDL2
add_subdirectory("external/SDL2")

## SDL_image
add_subdirectory("external/SDL_image")

## Compile
add_executable(${PROJECT_NAME} "src/main.cpp")
target_link_libraries(${PROJECT_NAME} SDL2::SDL2-static SDL2_image::SDL2_image)

It works fine for just SDL2 (as long as I statically link it. I've got no idea why it's not working. Here's my code:

#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include <SDL_image.h>
#include <iostream>

int main(int argc, char *argv[]) { // thanks, https://trenki2.github.io/blog/2017/06/02/using-sdl2-with-cmake/
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow(
        "Pacman",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        1920,
        1080,
        0
    );

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    // this doesn't run if the IMG_Init line is uncommented
    printf("working up to here");

    // this is the troublesome line. I'm guessing that SDL_Image is only linked when functions from it are used, and something with SDL_Image breaks my program?
    // // Initialize PNG loading
    // IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);

    SDL_Delay(3000);
    
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

Upvotes: 0

Views: 45

Answers (0)

Related Questions