首頁  >  文章  >  後端開發  >  如何將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: SDLPK2_image,它與DULE_ARCSE三個參數對齊。

以上是如何將SDL2和SDL_image與CMake整合?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn