Search code examples
c++cmakesdlsdl-2

Compiled C++ build doesn't output nor provide error


I'm using cmake and mingw32-make to build my C++ SDL2 project called Something.
After I did cmake . it generated few folders and files, where I went inside the build folder. It had a make file, so I used make in that directory.
Then it generated my executable which should be something.exe. It did, but when I try to run the executable it provides no errors or output. When I try to compile without including SDL.h and commenting the sdl code, it outputs Initialized which should appear even with including SDl.h.
This is my project directory

Something
  |
  |
  |---- SDL2
  |       |
  |       |---- include
  |       |
  |       |---- lib
  |
  ---- src
  |     |
  |     |---- something.cpp
  |
  |
  |---- CMakeLists.txt

This is my cmake file

# cmake version to be used
cmake_minimum_required( VERSION 3.8.0 )
project(something VERSION 1.0.0)

set(SDL2_DIR "${CMAKE_SOURCE_DIR}/SDL2/")

set(SDL2_INCLUDE_DIRS "${SDL2_DIR}/include")


# Support both 32 and 64 bit builds
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
    set(SDL2_LIBRARIES "${SDL2_DIR}/lib/x64/SDL2main.lib;${SDL2_DIR}/lib/x64/SDL2.lib")
else ()
    set(SDL2_LIBRARIES "${SDL2_DIR}/lib/x86/SDL2main.lib;${SDL2_DIR}/lib/x86/SDL2.lib")
endif ()

# link dependencies
include_directories(${SDL2_INCLUDE_DIRS})
link_directories(${SDL2_LIBRARIES})

# link dependencies
include_directories(${SDL2_INCLUDE_DIRS})
link_directories(${SDL2_LIBRARIES})

# Project files and linking
set(SOURCES src/something.cpp)
add_executable(${PROJECT_NAME} src/something.cpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})

And finally this is my src/something.cpp

#include <iostream>
#include <SDL.h>

int main(int agrc, char* agrs[]){
    std::cout << "Initialized!!" << std::endl;  
    
    SDL_Window* window = NULL;
    SDL_Renderer* renderer = NULL;

    if ( SDL_Init(SDL_INIT_EVERYTHING) != 0 ){
            std::cout << "Error : " << SDL_GetError() << std::endl;
            return -1;
    }    
    
    window = SDL_CreateWindow("Something", 50, 50, 700, 500, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_Delay(1000);
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
    return 0;
}

Solution

  • Must not forget to put the .dll(dynamic link libraries) in the same directory as the executable in order for the executable to work