简体   繁体   English

如何在 cmake 中使用 SDL2 和 SDL_image

[英]How to use SDL2 and SDL_image with cmake

I'm looking for the simplest way to compile a c++ program using SDL2 and SDL_image with cmake.我正在寻找使用带有 cmake 的SDL2SDL_image编译 C++ 程序的最简单方法。

Here is my best attempt, after hours of searching:经过数小时的搜索,这是我最好的尝试:

CMakeLists.txt CMakeLists.txt

project(shooter-cmake2)

cmake_minimum_required(VERSION 2.8)

set(SOURCES
shooter.cpp
classes.cpp
utils.cpp
)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")

add_executable(${PROJECT_NAME} ${SOURCES})

INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2_image REQUIRED sdl2_image)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARY})

I get these errors:我收到这些错误:

In function `loadTexture(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, SDL_Renderer*)':
undefined reference to `IMG_LoadTexture'
collect2: ld returned 1 exit status

Here is the function call:这是函数调用:

#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;
}

I think that the following will work, as it finds the libraries on my ubuntu system and the example function you provided can link:我认为以下内容会起作用,因为它会在我的 ubuntu 系统上找到库,并且您提供的示例函数可以链接:

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})

If cmake is executed with --debug-output it outputs:如果使用 --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

This made me check the contents of这让我检查了内容

/usr/lib/x86_64-linux-gnu/pkgconfig/sdl2.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/SDL2_image.pc

I noticed that SDL2_image.pc contains Name: SDL2_image which I assumed should match the third parameter to PKG_SEARCH_MODULE for this library.我注意到 SDL2_image.pc 包含 Name: SDL2_image 我认为应该将第三个参数与该库的 PKG_SEARCH_MODULE 匹配。

There are two blog posts about this here:这里有两篇关于此的博客文章:

Using SDL2 with CMake 在 CMake 中使用 SDL2

Using SDL2_image with CMake 在 CMake 中使用 SDL2_image

Basically you need a FindSDL2.cmake and FindSDL2_image.cmake module.基本上你需要一个FindSDL2.cmakeFindSDL2_image.cmake模块。 They can be based of the ones that work for SDL 1.2 which are included in CMake already.它们可以基于已包含在 CMake 中的适用于 SDL 1.2 的那些。 Using these Find modules will also work on Windows.使用这些 Find 模块也适用于 Windows。

If you are on Linux and only need SDL2 you don't even need the FindSDL2.cmake as the following already works:如果您使用的是 Linux 并且只需要 SDL2,您甚至不需要FindSDL2.cmake因为以下已经有效:

cmake_minimum_required(VERSION 3.7)

project(SDL2Test)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

add_executable(SDL2Test Main.cpp)
target_link_libraries(SDL2Test ${SDL2_LIBRARIES})

I was having trouble with these answers, I think cmake changed the way to import targets.我在回答这些问题时遇到了麻烦,我认为 cmake 改变了导入目标的方式。 Following @trenki blog post I needed to change my CMakeLists.txt to:在@trenki 博客文章之后,我需要将我的 CMakeLists.txt 更改为:

project(SDL2Test)
find_package(SDL2 REQUIRED COMPONENTS SDL2::SDL2)
add_executable(SDL2Test main.cpp)
target_link_libraries(SDL2Test SDL2::SDL2)

Currently this works out of the box on Arch Linux.目前,这在 Arch Linux 上开箱即用。

I introduced a modern and portable approach for linking to the SDL2, SDL2_image.我介绍了一种用于链接到 SDL2、SDL2_image 的现代且可移植的方法。 These SDL2 CMake modules let you build an SDL2 & SDL2_image project as follows :这些SDL2 CMake 模块可让您构建 SDL2 和 SDL2_image 项目,如下所示:

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)

find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2::Image)

You should just clone the repo in your project:你应该只在你的项目中克隆 repo:

git clone https://github.com/aminosbh/sdl2-cmake-modules cmake/sdl2

Note: If CMake didn't find the SDL2/SDL2_image libraries (in Windows), we can specify the CMake options SDL2_PATH and SDL2_IMAGE_PATH as follows:注意:如果 CMake 没有找到 SDL2/SDL2_image 库(在 Windows 中),我们可以指定 CMake 选项SDL2_PATHSDL2_IMAGE_PATH如下:

cmake .. -DSDL2_PATH="/path/to/sdl2" -DSDL2_IMAGE_PATH="/path/to/sdl2-image"

It supports also other related libraries : SDL2_ttf, SDL2_net, SDL2_mixer and SDL2_gfx.它还支持其他相关库:SDL2_ttf、SDL2_net、SDL2_mixer 和 SDL2_gfx。 For more details, please read the README.md file.有关更多详细信息,请阅读README.md文件。

You can find a list of examples/samples and projects that uses these modules here : https://github.com/aminosbh/sdl-samples-and-projects您可以在此处找到使用这些模块的示例/示例和项目列表: https : //github.com/aminosbh/sdl-samples-and-projects

The following commands works fine for me:以下命令对我来说很好用:

set(SDL_INCLUDE_DIR "/usr/include/SDL2")
set(SDL_LIBRARY "SDL2")
include(FindSDL)  

if(SDL_FOUND)  
  message(STATUS "SDL FOUND")
endif()

Since 2.6 version, SDL2_image installation is shipped with CMake config script SDL2_imageConfig.cmake / SDL2_image-config.cmake .从 2.6 版本开始,SDL2_image 安装随 CMake 配置脚本SDL2_imageConfig.cmake / SDL2_image-config.cmake一起提供。

So find_package(SDL2_image) works without any additional FindSDL2_image.cmake module, and creates IMPORTED target SDL2_image::SDL2_image :因此find_package(SDL2_image)无需任何额外FindSDL2_image.cmake模块即可工作,并创建 IMPORTED 目标SDL2_image::SDL2_image

find_package(SDL2_image REQUIRED)
target_link_libraries(<executable-target> SDL2_image::SDL2_image)

Note, that variables like SDL2_IMAGE_LIBRARIES or SDL2_IMAGE_INCLUDE_DIRS are NOT set in this case, so using them is meaningless.请注意,在这种情况下未设置像SDL2_IMAGE_LIBRARIESSDL2_IMAGE_INCLUDE_DIRS这样的变量,因此使用它们是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM