Search code examples
visual-studiovisual-studio-codedlllinkersdl

Avoid Modifying Compiler and Linker Settings every time to use SDL in projects


I have recently started with my plan to develop a simple game and for that, I have installed SDL for VS. But Every time I create a new project I have to go to properties-> Compiler and properties-> Linker of that project to add my SDL libraries. Can I permanently include these settings. Also, does it necessary to include ".dll" every time in my project or I can link them too for anytime usage.


Solution

  • I have been using CMake with VS and SDL lately and it reduces this annoyance a bit. Your top directory (and additional directories, depending on how you organize your project) will have a CMakeLists.txt file which handles include directories, finding libraries and DLLs, and building any dependencies. The one for my current project looks like this:

    cmake_minimum_required(VERSION 3.19)
    
    project(chip8emu VERSION 1.0 DESCRIPTION "Chip-8 Emulator" LANGUAGES CXX)
    
    set(SDL2_INCLUDE_DIR "C:/dev/vclibs/SDL2-2.0.14/include")
    set(SDL2_LIBRARY "C:/dev/vclibs/SDL2-2.0.14/lib/x64")
    
    FIND_PACKAGE(Boost REQUIRED COMPONENTS log)
    FIND_PACKAGE(wxWidgets)
    ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
    INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${wxWidgets_INCLUDE_DIRS})
    INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIR})
    INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})
    add_executable(chip8emu chip8emu.cpp chip8.cpp chip8.h "logger.h" "logger.cpp" "emuWindow.h" "emuWindow.cpp" "beeper.h" "beeper.cpp" "cApp.h" "cApp.cpp" "cMain.h" "cMain.cpp" )
    
    target_link_libraries(chip8emu PRIVATE Boost::log_setup Boost::log ${SDL2_LIBRARY}/SDL2main.lib ${SDL2_LIBRARY}/SDL2.lib ${wxWidgets_LIB_DIR})
    

    I can basically copy and paste this into each project directory (changing the add_executable() line).

    There are positives and negatives to this approach, but I like it because it makes it easy to integrate libraries and it makes me less dependent on VS. This is a good tutorial to start with (although it looks intimidating, its pretty simple).