NOTE This is not a compile time issue this is a cmake generation-time issue.
I've been trying to setup my environment so that cmake can find SDL2.
I am on windows 11, with the latest versions of SDL2 (found here), SDL_image (found here) and SDL_ttf (found here) downloaded to the folder C:/ProgramFilesPublic/
. I used the respective mingw zips for that.
However no matter what I try if its not an issue with finding SDL2 then it can never find SDL2_image or SDL2_ttf no matter what I try.
I tried using find_package(SDL2 REQUIRED)
and find_package(SDL2_image REQUIRED)
etc.,
then find_package(SDL2 REQUIRED PATHS "C:/ProgramFilesPublic/")
and find_package(SDL2_image REQUIRED PATHS "C:/ProgramFilesPublic/SDL_image")
,
then a few other combinations of that changing that path or adding hints in case that would do it,
then I decided to try https://gitlab.com/aminosbh/sdl2-cmake-modules.git with set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${SDL_FIND_MODULE_PATH})
and find_package(SDL2 REQUIRED)
and find_package(SDL2_image REQUIRED)
etc.
I also tried modifying all the paths involved to see if giving it a list of specific files or the directory would fix it, and that didnt do it either.
While none of these worked with SDL2_image or SDL2_ttf, the last at least worked with SDL2. The last one also gives me an error along the lines of
1> [CMake] By not providing "FindSDL2_image.cmake" in CMAKE_MODULE_PATH this project
1> [CMake] has asked CMake to find a package configuration file provided by
1> [CMake] "SDL2_image", but CMake did not find one.
1> [CMake]
1> [CMake] Could not find a package configuration file provided by "SDL2_image" with
1> [CMake] any of the following names:
1> [CMake]
1> [CMake] SDL2_imageConfig.cmake
1> [CMake] sdl2_image-config.cmake
1> [CMake]
1> [CMake] Add the installation prefix of "SDL2_image" to CMAKE_PREFIX_PATH or set
1> [CMake] "SDL2_image_DIR" to a directory containing one of the above files. If
1> [CMake] "SDL2_image" provides a separate development package or SDK, be sure it has
1> [CMake] been installed.
Frankly I have no idea why this isn't working. I even checked CMAKE_MODULE_PATH
and it refuses to work even when it has EVERY possible combination of path to the FindSDL2*.cmake
files needed AT ONCE and even tried adding the /lib
directory from each SDL folder in my C:/ProgramFilesPublic/
folder. I've verified this by message("${CMAKE_MODULE_PATH}")
witch shows this.
UPDATE: I have currently gotten this far (SDL can be found, however SDL_image and SDL_ttf can't)
# CMakeList.txt : CMake project for libcgui, include source and define
# project specific logic here.
cmake_minimum_required (VERSION 3.10)
if (CMAKE_C_CPPCHECK)
find_program(CMAKE_C_CPPCHECK NAMES cppcheck PATHS "C:/Program Files/Cppcheck" "C:/Program Files (x86)/Cppcheck")
endif()
if(CMAKE_C_CPPCHECK)
list(APPEND CMAKE_C_CPPCHECK "--enable=all" "--force" "--inline-suppr")
else()
set(CMAKE_C_CPPCHECK "")
endif()
set(SDL_MIN_VER 2.0.0 CACHE VERSION "The minimum required version for SDL")
set(SDL_IMAGE_MIN_VER 2.0.5 CACHE VERSION "The minimum required version for SDL_image")
set(SDL_TTF_MIN_VER 2.20.2 CACHE VERSION "The minimum required version for SDL_ttf")
set(SDL_FIND_MODULE_PATH "lib/sdl2-cmake-modules" CACHE PATH "The Paths leading to the SDL Finding CMake Modules") #TODO
set(SDL2_PATH "C:/ProgramFilesPublic/SDL2" CACHE PATH "The location to search for SDL2")
set(SDL2_ttf_PATH "C:/ProgramFilesPublic/SDL2_ttf" CACHE PATH "The location to search for SDL2_ttf")
set(SDL2_image_PATH "C:/ProgramFilesPublic/SDL2_image" CACHE PATH "The location to search for SDL2_image")
get_filename_component(SDL_FIND_MODULE_PATH ${SDL_FIND_MODULE_PATH} ABSOLUTE)
get_filename_component(SDL2_PATH ${SDL2_PATH} ABSOLUTE)
get_filename_component(SDL2_ttf_PATH ${SDL2_ttf_PATH} ABSOLUTE)
get_filename_component(SDL2_image_PATH ${SDL2_image_PATH} ABSOLUTE)
list(APPEND CMAKE_MODULE_PATH ${SDL_FIND_MODULE_PATH})
message("${CMAKE_MODULE_PATH}")
list(APPEND CMAKE_PREFIX_PATH ${SDL2_PATH})
list(APPEND CMAKE_PREFIX_PATH ${SDL2_ttf_PATH})
list(APPEND CMAKE_PREFIX_PATH ${SDL2_image_PATH})
project ("libcgui" LANGUAGES C)
#locate sdl and set its variables
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL2_image REQUIRED)
message("AAAAAAAAAAAAAAA ${SDL2_image_LIBRARIES} ${SDL2_ttf_LIBRARIES} ${SDL2_LIBRARIES}")
#add the macros:
set(NO_PRAGMA OFF CACHE BOOL "Disable the use of the #pragma macro. Only useful if your build system doesn't like using it.")
set(NO_HAS_INCLUDE OFF CACHE BOOL "Disable the use of the __has_include() macro. Only useful if your build system doesn't like using it.")
set(NO_AUTO_IMPORT OFF CACHE BOOL "Stop trying to import external dependencies in the headers, expect that they will be always imported manually instead.")
#global build settings
#set_property(GLOBAL PROPERTY C_STANDARD_REQUIRED ON)
#set_property(GLOBAL PROPERTY C_STANDARD 90)
set(MSVC_EXTRA_OPTIMISE $<MSVC AND MSVC_VERSION GREATER 1900> CACHE BOOL "Enable optimizations for Win32 based platforms")
set(GNUCC_EXTRA_OPTIMISE $<CMAKE_COMPILER_IS_GNUCC> CACHE BOOL "Enable optimizations for Unix/Linux based platforms")
if (NOT SDL2_image_FOUND OR NOT SDL2_ttf_FOUND OR NOT SDL2_FOUND)
message("SDL and/or it submodules couldn't be found on this system. One solution may be to install sdl development libs manually via the SDL official website binary or another may be to use your package manager.")
if (WIN32)
message("For your system I'd suggest downloading the binaries directly")
else()
message("For your system I'd suggest checking your package manager first (if possible)")
endif()
#TODO return a failure
#TODO separate errors for each submodule into it own helpful error
endif()
#main executable
add_executable (${PROJECT_NAME} "libcgui.c" "libcgui.h" "libcgui_widgets.h" "libcgui_fallbacks.h" "libcgui_backend_SDL2.h" "libcgui_widgets.c" "libcgui_backend_SDL2.c" "libcgui_backend_helpers.h")
#target_include_directories(${PROJECT_NAME} PUBLIC ${SDL2_INCLUDE_DIRS} ${SDL2_ttf_INCLUDE_DIRS} ${SDL2_image_INCLUDE_DIRS})
target_include_directories(${PROJECT_NAME} PUBLIC "lib/")
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${SDL2_image_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${SDL2_ttf_LIBRARIES})
if (NO_PRAGMA)
target_compile_definitions(${PROJECT_NAME} PUBLIC NO_PRAGMA)
endif()
if (NO_HAS_INCLUDE)
target_compile_definitions(${PROJECT_NAME} PUBLIC NO_HAS_INCLUDE)
endif()
if (NO_AUTO_IMPORT)
target_compile_definitions(${PROJECT_NAME} PUBLIC NO_AUTO_IMPORT)
endif()
if (CMAKE_COMPILER_IS_GNUCC)
#from https://stackoverflow.com/questions/2855121/what-is-the-purpose-of-using-pedantic-in-the-gcc-g-compiler
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wpedantic -Wextra)
endif()
if (GNUCC_EXTRA_OPTIMISE)
#from https://wiki.gentoo.org/wiki/GCC_optimization
target_compile_options(${PROJECT_NAME} PRIVATE -O3 -fPIC)
endif()
if(MSVC AND MSVC_VERSION GREATER 1900)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall)
if (CMAKE_BUILD_TYPE STREQUAL Debug)
target_link_options(${PROJECT_NAME} PRIVATE -VERBOSE)
endif()
endif()
if (MSVC_EXTRA_OPTIMISE)
#from https://stackoverflow.com/questions/53160599/enable-qspectre-and-control-flow-guard-switch-in-cmake
message("Setting CONTROL FLOW GUARD and QSPECTERE")
target_compile_options(${PROJECT_NAME} PRIVATE -GUARD:CF -Qspectre -Ot )
target_link_options(${PROJECT_NAME} PRIVATE -GUARD:CF -DYNAMICBASE)
if (CMAKE_BUILD_TYPE STREQUAL Debug)
target_link_options(${PROJECT_NAME} PRIVATE -OPT:REF,ICF,LBR)
endif()
endif()