简体   繁体   中英

Executable can't find SDL2.dll

I have been trying to figure out why CMake is not adding the static libraries of SDL2. If I add the SDL2.dll file next to the executable, then it does work.

It al builds fine and the headers can be found nice and easily.

The error

My CMakeList.txt looks like this. Trust me, I have tried everything but clearly I am doing something wrong.

cmake_minimum_required(VERSION 3.7)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

SET(SDL2_DIR ./lib)

project(SDL2Test)

find_package(SDL2 REQUIRED)
include_directories(SDL2Test ${SDL2_INCLUDE_DIRS})

add_executable(SDL2Test Main.cpp)
target_link_libraries(SDL2Test ${SDL2_LIBRARIES})

This is the sdl2-config.cmake:

set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/../include")

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

string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)

This is the Main.cpp

#include "SDL.h"

using namespace std;

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow(
            "SDL2Test",
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            640,
            480,
            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);

    SDL_Delay(3000);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

Your SDL2.lib is import library, that requires accompanying DLL in the runtime.

To use static library (one that is completely linked with your EXE and does not require DLL), you will need to build static version of SDL2.lib yourself.

Keep in mind that this is discouraged by the author of SDL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM