cmake CLion、MinGW和SDL 2:进程已完成,退出代码为-1073741515(0xC 0000135)

8mmmxcuj  于 2023-05-07  发布在  其他
关注(0)|答案(3)|浏览(272)

我正在尝试将SDL 2添加到我的CLion项目中。我发现了这个guide,并试图遵循它,同时只包括SDL 2。一切都编译,但当我启动我的应用程序时,我得到“Process finished with exit code -1073741515(0xC 0000135)”。
在我的CMakeLists.txt文件中:

cmake_minimum_required(VERSION 3.15)
project(Test)
    
set(CMAKE_CXX_STANDARD 14)
    
set(CMAKE_CXX_FLAGS "-lmingw32 -static-libgcc -static-libstdc++")
set(SDL2_PATH "C:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "C:/CPP/libs/CMakeModules")
    
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
    
if (${SDL2_FOUND})
    message(VERBOSE, "sdl found!")
else ()
    message(FATAL_ERROR, "sdl not found")
endif ()
    
message(VERBOSE, ${SDL2_INCLUDE_DIR})
message(VERBOSE, ${SDL2_LIBRARY})
    
add_executable(Test src/main.cpp)
target_link_libraries(Test ${SDL2_LIBRARY})

main.cpp:

#include <SDL.h>
#include <cstdio>

int main(int argc, char *args[]) {

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    }
    SDL_Quit();
    return 0;
}

我使用CLion 2019.3.2与捆绑的CMake,最新的MinGW构建(x86_64-8.1.0-win32-seh-rt_v6-rev 0)和最新的SDL 2(2.0.10)。CMake输出看起来也正常:

VERBOSE,sdl found!
VERBOSE,C:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32/include/SDL2
VERBOSE,mingw32-mwindowsC:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32/lib/libSDL2main.aC:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32/lib/libSDL2.dll.a-lpthread
zlhcx6iw

zlhcx6iw1#

如果您在CLion中使用Visual Studio工具链:
您需要在文件夹cmake-build-debugcmake-build-release中粘贴文件.dll,但不仅仅是SDL2_image.dll,文件夹lib/x86中的所有文件
SDL2_image-devel-2.0.5-VC.zip

ctehm74n

ctehm74n2#

如果问题是在运行时找不到您的DLL,并且您希望将SDL DLL复制到同一目录,则可以执行以下操作(另请参见How to copy DLL files into the same folder as the executable using CMake?):

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

参见:

  • https://cmake.org/cmake/help/latest/variable/WIN32.html
  • https://cmake.org/cmake/help/latest/command/add_custom_command.html#build-events
  • https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-E-arg-copy_if_different
  • https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#target-dependent-expressions

与此解决方案相关的还有:https://github.com/libsdl-org/SDL/issues/6399

7eumitmz

7eumitmz3#

我的CMakeLists.txt似乎与你的不同。下面是我的配置,希望能对你有所帮助。

cmake_minimum_required(VERSION 3.15)
project(cppSDL)

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLIGS "${CMAKE_CXX_FLAGS} -std=c++17 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib/x86)

set(SOURCE_FILES SDLtutorial.cpp)
add_executable(SDLtutorial ${SOURCE_FILES})

target_link_libraries(SDLtutorial SDL2main SDL2 SDL2_ttf SDL2_image)

相关问题