如何在cmake中使用SDL2和SDL_image

问题描述 投票:22回答:5

我正在寻找使用SDM2和SDL_image与cmake编译c ++程序的最简单方法。

经过几个小时的搜索后,这是我最好的尝试:

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

我收到这些错误:

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

这是函数调用:

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

我很绝望。请帮我!谢谢! :)

c++ cmake sdl sdl-2 sdl-image
5个回答
37
投票

我认为以下内容可行,因为它在我的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})

如果使用--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

这让我检查了内容

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

我注意到SDL2_image.pc包含Name:SDL2_image,我假设它应该匹配此库的PKG_SEARCH_MODULE的第三个参数。


6
投票

这里有两篇博客文章:

Using SDL2 with CMake

Using SDL2_image with CMake

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

如果您使用的是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})

4
投票

我对这些答案遇到了麻烦,我认为cmake改变了导入目标的方式。在@trenki博客文章后,我需要将我的CMakeLists.txt更改为:

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

目前,这在Arch Linux上开箱即用。


0
投票

以下命令对我来说很好:

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

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

0
投票

我介绍了一种现代便携式方法,用于链接SDL2,SDL2_image。这些SDL2 CMake modules允许您构建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)

您应该只是克隆项目中的repo:

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

注意:如果CMake没有找到SDL2 / SDL2_image库(在Windows中),我们可以指定CMake选项SDL2_PATHSDL2_IMAGE_PATH,如下所示:

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

它还支持其他相关库:SDL2_ttf,SDL2_net,SDL2_mixer和SDL2_gfx。有关更多详细信息,请阅读README.md文件。

您可以在此处找到使用这些模块的示例/示例和项目列表:https://github.com/aminosbh/sdl-samples-and-projects

© www.soinside.com 2019 - 2024. All rights reserved.