C++ 如何在 cmake 中使用 SDL2 和 SDL_image

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23850472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:34:16  来源:igfitidea点击:

How to use SDL2 and SDL_image with cmake

c++cmakesdlsdl-2sdl-image

提问by Carl Lemaire

I'm looking for the simplest way to compile a c++ program using SDL2and SDL_imagewith cmake.

我正在寻找使用SDL2SDL_image和 cmake编译 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 am desperate. Please help me! Thanks! :)

生无可恋。请帮我!谢谢!:)

回答by wojciii

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 匹配。

回答by trenki

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.cmakeand FindSDL2_image.cmakemodule. They can be based of the ones that work for SDL 1.2 which are included in CMake already. Using these Find modules will also work on Windows.

基本上你需要一个FindSDL2.cmakeFindSDL2_image.cmake模块。它们可以基于已包含在 CMake 中的适用于 SDL 1.2 的那些。使用这些 Find 模块也适用于 Windows。

If you are on Linux and only need SDL2 you don't even need the FindSDL2.cmakeas 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})

回答by Charles

I was having trouble with these answers, I think cmake changed the way to import targets. Following @trenki blog post I needed to change my CMakeLists.txt to:

我在回答这些问题时遇到了麻烦,我认为 cmake 改变了导入目标的方式。在@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 上开箱即用。

回答by adem

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

回答by aminosbh

I introduced a modern and portable approach for linking to the SDL2, SDL2_image. These SDL2 CMake moduleslet you build an SDL2 & SDL2_image project as follows :

我介绍了一种用于链接到 SDL2、SDL2_image 的现代且可移植的方法。这些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_PATHand SDL2_IMAGE_PATHas 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. For more details, please read the README.mdfile.

它还支持其他相关库:SDL2_ttf、SDL2_net、SDL2_mixer 和 SDL2_gfx。有关更多详细信息,请阅读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