SDL2 构建错误 C2118:负下标

SDL2 build error C2118: negative subscript

我一直在尝试使用 CMake + CLion + MSVC 构建 SDL2,但我遇到了这个错误:

FAILED: SDL2/CMakeFiles/SDL2.dir/src/dynapi/SDL_dynapi.c.obj 
C:\PROGRA~2\MICROS~119\ENTERP~1\VC\Tools\MSVC29~1.301\bin\Hostx86\x86\cl.exe  /nologo -D_THREAD_SAFE -IC:\Users\mprze\source\repos\ComputerGraphics\ComputerGraphics\dependencies\SDL2\include /DWIN32 /D_WINDOWS /W3 /MDd /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoSDL2\CMakeFiles\SDL2.dir\src\dynapi\SDL_dynapi.c.obj /FdSDL2\CMakeFiles\SDL2.dir\SDL2.pdb /FS -c C:\Users\mprze\source\repos\ComputerGraphics\ComputerGraphics\dependencies\SDL2\src\dynapi\SDL_dynapi.c
C:\Program Files (x86)\Windows Kits\include.0.19041.0\um\winnt.h(2501): error C2118: negative subscript

Visual Studio 2019年搭建没问题

我从 https://github.com/libsdl-org/SDL and taken the CMake script from https://github.com/google/filament/blob/main/third_party/libsdl2/tnt/CMakeLists.txt 克隆了 SDL2,但删除了与 Windows 无关的所有内容。 现在我只想让它在 windows 上运行,然后我会担心其他平台。

错误日志说明了一些关于 add_definitions(-D_THREAD_SAFE) 的内容,因此我尝试将其注释掉并重建 SDL2 库。它产生此消息:

FAILED: SDL2/CMakeFiles/SDL2.dir/src/dynapi/SDL_dynapi.c.obj 
C:\PROGRA~2\MICROS~119\ENTERP~1\VC\Tools\MSVC29~1.301\bin\Hostx86\x86\cl.exe  /nologo  -IC:\Users\mprze\source\repos\ComputerGraphics\ComputerGraphics\dependencies\SDL2\include /DWIN32 /D_WINDOWS /W3 /MDd /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoSDL2\CMakeFiles\SDL2.dir\src\dynapi\SDL_dynapi.c.obj /FdSDL2\CMakeFiles\SDL2.dir\SDL2.pdb /FS -c C:\Users\mprze\source\repos\ComputerGraphics\ComputerGraphics\dependencies\SDL2\src\dynapi\SDL_dynapi.c
C:\Program Files (x86)\Windows Kits\include.0.19041.0\um\winnt.h(2501): error C2118: negative subscript

该消息说了一些关于 SDL_dynapi.c 的内容,所以我尝试将其注释掉,看看会发生什么。

它建立了! 但是 当我想 link 它进入一个可执行的应用程序时,在 Visual Studio 2019 上有一堆错误(是的,现在它在 VS2019 上也不起作用) 或克里昂... 在 Visual studio 上有 LNK unresolved external symbol SDL_XXX,在 CLion 上有这条大消息

FAILED: ComputerGraphics/dependencies/ImGui/CMakeFiles/ImGui.dir/backends/imgui_impl_sdl.cpp.obj 
C:\PROGRA~2\MICROS~119\ENTERP~1\VC\Tools\MSVC29~1.301\bin\Hostx86\x86\cl.exe  /nologo /TP  -IC:\Users\mprze\source\repos\ComputerGraphics\ComputerGraphics\dependencies\ImGui -IC:\Users\mprze\source\repos\ComputerGraphics\ComputerGraphics\dependencies\ImGui\backends -IC:\Users\mprze\source\repos\ComputerGraphics\ComputerGraphics\dependencies\SDL2\include /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 -std:c++20 /showIncludes /FoComputerGraphics\dependencies\ImGui\CMakeFiles\ImGui.dir\backends\imgui_impl_sdl.cpp.obj /FdComputerGraphics\dependencies\ImGui\CMakeFiles\ImGui.dir\ImGui.pdb /FS -c C:\Users\mprze\source\repos\ComputerGraphics\ComputerGraphics\dependencies\ImGui\backends\imgui_impl_sdl.cpp
C:\Program Files (x86)\Windows Kits\include.0.19041.0\um\winnt.h(2496): error C2338: Windows headers require the default packing option. Changing this can lead to memory corruption. This diagnostic can be disabled by building with WINDOWS_IGNORE_PACKING_MISMATCH defined.

我尝试了不同的方法并总结了我不知道如何解决它。

我希望使用 Microsoft c++ 编译器在任何 IDE 上都能达到 运行。 我怎样才能让它发挥作用?

SDL2 CMake 脚本

cmake_minimum_required(VERSION 3.8)
project(SDL2 C)

set(OUR_DIR ${CMAKE_CURRENT_SOURCE_DIR})

set(TARGET              SDL2)
set(SRC_DIR             ${OUR_DIR}/src)
set(PUBLIC_HDR_DIR      ${OUR_DIR}/include)

file(GLOB SRCS
    ${SRC_DIR}/*.c
    ${SRC_DIR}/atomic/*.c
    ${SRC_DIR}/audio/*.c
    ${SRC_DIR}/audio/disk/*.c
    ${SRC_DIR}/audio/dsp/*.c
    ${SRC_DIR}/audio/dummy/*.c
    ${SRC_DIR}/cpuinfo/*.c
    ${SRC_DIR}/dynapi/*.c
    ${SRC_DIR}/events/*.c
    ${SRC_DIR}/file/*.c
    ${SRC_DIR}/joystick/*.c
    ${SRC_DIR}/joystick/steam/*.c
    ${SRC_DIR}/haptic/*.c
    ${SRC_DIR}/libm/*.c
    ${SRC_DIR}/power/*.c
    ${SRC_DIR}/render/*.c
    ${SRC_DIR}/render/*/*.c
    ${SRC_DIR}/stdlib/*.c
    ${SRC_DIR}/thread/*.c
    ${SRC_DIR}/timer/*.c
    ${SRC_DIR}/video/*.c
    ${SRC_DIR}/video/yuv2rgb/*.c
    ${SRC_DIR}/video/dummy/*.c
)

if (WIN32)
    file(GLOB SRCS_WIN32
            ${SRC_DIR}/core/windows/*.c
            ${SRC_DIR}/video/windows/*.c
            ${SRC_DIR}/thread/windows/*.c
            ${SRC_DIR}/thread/generic/SDL_syscond.c 
            ${SRC_DIR}/power/windows/*.c
            ${SRC_DIR}/filesystem/windows/*.c
            ${SRC_DIR}/timer/windows/*.c
            ${SRC_DIR}/joystick/windows/*.c
            ${SRC_DIR}/haptic/windows/*.c
            ${SRC_DIR}/loadso/windows/*.c
            ${SRC_DIR}/audio/winmm/*.c
            ${SRC_DIR}/audio/wasapi/*.c
    )
    set(SRCS ${SRCS} ${SRCS_WIN32})

    list(APPEND EXTRA_LIBS dinput8)
    list(APPEND EXTRA_LIBS dxguid)
    list(APPEND EXTRA_LIBS gdi32)
    list(APPEND EXTRA_LIBS imagehlp)
    list(APPEND EXTRA_LIBS imm32)
    list(APPEND EXTRA_LIBS ole32)
    list(APPEND EXTRA_LIBS oleaut32)
    list(APPEND EXTRA_LIBS shell32)
    list(APPEND EXTRA_LIBS user32)
    list(APPEND EXTRA_LIBS uuid)
    list(APPEND EXTRA_LIBS version)
    list(APPEND EXTRA_LIBS winmm)

endif()

include_directories(
    ${PUBLIC_HDR_DIR}
)

add_definitions(-D_THREAD_SAFE)

add_library(${TARGET} STATIC ${SRCS})

target_link_libraries(${TARGET} ${EXTRA_LIBS})

target_include_directories(${TARGET} PUBLIC ${PUBLIC_HDR_DIR})

if (WIN32)
    message(STATUS "win32")
    add_library(${TARGET}main ${SRC_DIR}/main/windows/SDL_windows_main.c)
endif()

我需要将 SDL link编辑到库中,然后将库link编辑到可执行文件中。

库 CMake 脚本

cmake_minimum_required(VERSION 3.8)
project(ComputerGraphics)

set(TARGET ComputerGraphics)

add_subdirectory("dependencies")

add_library (${TARGET} "Application.cpp")

target_link_libraries(${TARGET}
    PUBLIC
        SDL2
        SDL2main
)

target_include_directories(${TARGET}
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}
)

if (WIN32)
    target_link_libraries(${TARGET} PRIVATE opengl32 gdi32)
endif()

可执行应用程序 CMake 脚本

cmake_minimum_required(VERSION 3.8)

project("Line")

set(CMAKE_CXX_STANDARD 20)

add_executable (Line main.cpp)

target_link_libraries(Line
    PUBLIC
        ComputerGraphics
)

我为 x86 构建了它。对于 x64,它工作正常。