Search code examples
c++visual-c++cmakesdlclion

SDL2 build error C2118: negative subscript


I've been trying to build SDL2 using CMake + CLion + MSVC and I've got this error:

FAILED: SDL2/CMakeFiles/SDL2.dir/src/dynapi/SDL_dynapi.c.obj 
C:\PROGRA~2\MICROS~1\2019\ENTERP~1\VC\Tools\MSVC\1429~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\10\include\10.0.19041.0\um\winnt.h(2501): error C2118: negative subscript

Visual Studio 2019 builds it without any problems

I've cloned SDL2 from 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, but removed everything that is not related to Windows. Right now I just want it to work on windows, then I'll worry about other platforms.

The error log says something about add_definitions(-D_THREAD_SAFE), so I tried to comment it out and rebuild the SDL2 lib. It produces this message:

FAILED: SDL2/CMakeFiles/SDL2.dir/src/dynapi/SDL_dynapi.c.obj 
C:\PROGRA~2\MICROS~1\2019\ENTERP~1\VC\Tools\MSVC\1429~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\10\include\10.0.19041.0\um\winnt.h(2501): error C2118: negative subscript

The message says some about SDL_dynapi.c, so I tried to comment it out and see what happens.

And it builds! BUT When I want to link it into an executable app there are a bunch of errors either on Visual Studio 2019 (yes, now it also does not work on VS2019) or CLion... On Visual studio there are LNK unresolved external symbol SDL_XXX and on CLion there is this big message

FAILED: ComputerGraphics/dependencies/ImGui/CMakeFiles/ImGui.dir/backends/imgui_impl_sdl.cpp.obj 
C:\PROGRA~2\MICROS~1\2019\ENTERP~1\VC\Tools\MSVC\1429~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\10\include\10.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.

I tried different things and summarizing I don't know how to fix it.

I expect it to run on any IDE with the Microsoft c++ compiler. How can I make it work?

SDL2 CMake script

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()

I need SDL to be linked into the library and the library is linked into the executable.

Library CMake script

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()

the executable app CMake script

cmake_minimum_required(VERSION 3.8)

project("Line")

set(CMAKE_CXX_STANDARD 20)

add_executable (Line main.cpp)

target_link_libraries(Line
    PUBLIC
        ComputerGraphics
)

Solution

  • I built it for x86. For x64 it works fine.