How to Use SDL2 and SDL_image with CMake
To compile a C program using SDL2 and SDL_image with CMake, you can use the following steps:
<code class="cmake">project(shooter-cmake2) cmake_minimum_required(VERSION 2.8)</code>
<code class="cmake">set(SOURCES shooter.cpp classes.cpp utils.cpp )</code>
<code class="cmake">set(CMAKE_CXX_FLAGS "std=c++0x")</code>
<code class="cmake">add_executable(${PROJECT_NAME} ${SOURCES})</code>
<code class="cmake">INCLUDE(FindPkgConfig) PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)</code>
<code class="cmake">INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIR}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARY})</code>
Troubleshooting
If you encounter errors such as "undefined reference to IMG_LoadTexture," ensure that the required header files are included and the library is linked correctly. Verify that you have installed and configured SDL2 and SDL_image on your system. Additionally, consider checking the pkg-config files to confirm that the library names match the parameters specified in PKG_SEARCH_MODULE.
The above is the detailed content of How to Compile a C Program with SDL2 and SDL_image Using CMake?. For more information, please follow other related articles on the PHP Chinese website!