Including SDL2 Headers in CMake-Based Projects
This question tackles the issue of including SDL2 headers (#include "SDL.h") when creating a SDL2 project in CLion using CMake.
Solution
The provided solution offers two approaches for different operating systems:
Linux
For Linux, using a recent CMake version (e.g., 3.7) is sufficient. CMake's built-in find_package feature can locate and include SDL2 headers and libraries:
<code class="cmake">cmake_minimum_required(VERSION 3.7) project(SDL2Test) find_package(SDL2 REQUIRED) include_directories(SDL2Test ${SDL2_INCLUDE_DIRS}) add_executable(SDL2Test Main.cpp) target_link_libraries(SDL2Test ${SDL2_LIBRARIES})</code>
Windows
For Windows, follow these steps:
<code class="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}/lib/x64/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2main.lib") else () set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2main.lib") endif () string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)</code>
After these steps, you can include SDL2 headers using #include "SDL.h".
The above is the detailed content of How can I include SDL2 headers in a CMake-based project?. For more information, please follow other related articles on the PHP Chinese website!