Home >Backend Development >C++ >How to Integrate SDL2 and SDL_image with CMake?
Utilizing CMake to Integrate SDL2 and SDL_image
In this article, we will address the issue of compiling a C program that utilizes SDL2 and SDL_image using CMake. After thorough investigation, the following optimized CMake script emerged:
project(shooter-cmake2) cmake_minimum_required(VERSION 2.8) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") add_executable(${PROJECT_NAME} src/test.cpp) INCLUDE(FindPkgConfig) PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0) INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES})
This CMake script effectively locates the required libraries on Ubuntu systems, enabling the successful linking of the provided example function:
#include "SDL.h" #include "SDL_image.h" SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){ SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str()); texture != nullptr or die("LoadTexture"); return texture; }
Upon executing CMake with the --debug-output flag, the following output is generated:
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.26") Called from: [2] /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt -- checking for one of the modules 'sdl2' Called from: [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt -- checking for one of the modules 'SDL2_image>=2.0.0' Called from: [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
Further investigation of the corresponding pkgconfigs revealed that SDL2_image.pc contains the line Name: SDL2_image, which aligns with the third parameter of PKG_SEARCH_MODULE.
The above is the detailed content of How to Integrate SDL2 and SDL_image with CMake?. For more information, please follow other related articles on the PHP Chinese website!