利用 CMake 集成 SDL2 和 SDL_image
在本文中,我们将解决使用 SDL2 和 SDL_image 编译 C 程序的问题使用 CMake。经过彻底调查,出现了以下优化的 CMake 脚本:
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})
此 CMake 脚本有效地在 Ubuntu 系统上找到所需的库,从而能够成功链接所提供的示例函数:
#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; }
使用 --debug-output 标志执行 CMake 时,会生成以下输出:
-- 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
对相应 pkgconfigs 的进一步调查显示 SDL2_image.pc 包含行 Name: SDL2_image,它与PKG_SEARCH_MODULE 的第三个参数。
以上是如何将SDL2和SDL_image与CMake集成?的详细内容。更多信息请关注PHP中文网其他相关文章!