首页  >  文章  >  后端开发  >  如何将SDL2和SDL_image与CMake集成?

如何将SDL2和SDL_image与CMake集成?

Barbara Streisand
Barbara Streisand原创
2024-11-05 17:58:02611浏览

How to Integrate SDL2 and SDL_image with CMake?

利用 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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn