Search code examples
windowscmakesdl-2fetchcontent

SDL2d.dll not found on windows with CMake using fetchcontent


I am working on a cross platform application with CMake. The project builds on Linux, but not on windows. Any help would be appreciated.

The error I get is a popup that says:

The code execution cannot proceed because SDL2d.dll was not found. Reinstalling the program may fix this problem.

For some reason CMake doesn't set this directory to CMakeCache like it does for linux.

include(FetchContent)
message("Fetching SDL2...")
FetchContent_Declare(
    SDL2
    GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
    GIT_TAG release-2.26.3
)
FetchContent_MakeAvailable(SDL2)

As you can see I am getting this from the repository using fetchcontent. I also think it may be important to note that I am using pkgconfiglite package. I installed it using chocolatey package manager. While fetching SDL2 content I see this message in the output

pkg-config --static --libs sdl2" will return invalid information

It then goes on to list a bunch of SDL flags as on or off. I am assuming this is probably the reason for it, but as CMake seems to be using pkgconfig under the hood, this all seems like a blackbox that I don't know how to handle.

Below are my commands to make the executable and to link libraries.

add_executable(${Target}
    ${SRC_FILES}
)

target_link_libraries(${Target}
    Vulkan::Vulkan
    SDL2::SDL2
)

Solution

  • Problem solved. Problem has nothing to do with pkg-config.

    When developing cross-platform applications I must remember to keep in mind that Windows doesn't have dedicated locations for libraries and executables unlike Linux.

    Through my journey I have come up with 2 solutions. The second solution is much easier and cross-platform friendly but for completeness I will include both.

    The first requires adding the following to wherever your add_executable() function is called. It copies the location of the SDL2 dll to the location of your executable usinggenerator expressions "$<>". Generator expressions are evaluated at build time. My project has both a library and an executable. It didn't work when I tried to add it to the location of my dll.

    if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
        add_custom_command(
            TARGET yourgame POST_BUILD
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different "$<TARGET_FILE:SDL2::SDL2>" "$<TARGET_FILE_DIR:your_executable_name>"
            VERBATIM
        )
    endif()
    

    My project has both a library and an executable, but the above didn't work when I tried to add copy to my library.

    The second method requires me to move all my binaries to their own folder "${CMAKE_BINARY_DIR}/bin". This method will put your binaries in the same folder regardless of platform, allowing for a more cross-platform friendly experience. Your path to your program's assets will be identitcal. No generator expressions required. I added the below to my base CMakeLists.txt file.

    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)