如何将SDL2和SDL_image与cmake配合使用

yacmzcpb  于 2022-11-11  发布在  其他
关注(0)|答案(6)|浏览(288)

我正在寻找一种最简单的方法来编译一个c++程序使用SDL 2SDL_image与cmake。
经过几个小时的搜寻,我最好的尝试是:

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

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

gkl3eglg

gkl3eglg1#

我认为下面的代码可以工作,因为它可以在我的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包含名称:SDL2_image,我假设它应该与此库的PKG_SEARCH_MODULE的第三个参数匹配。

bjg7j2ky

bjg7j2ky2#

这里有两篇关于这方面的博客文章:
Using SDL2 with CMake
Using SDL2_image with CMake
基本上你需要一个FindSDL2.cmakeFindSDL2_image.cmake模块。它们可以基于CMake中已经包含的适用于SDL 1.2的模块。使用这些Find模块也可以在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})
brvekthn

brvekthn3#

我在回答这些问题时遇到了麻烦,我认为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上是开箱即用的。

wfypjpf4

wfypjpf44#

我介绍了一种现代的可移植方法,用于链接到SDL 2 SDL2_image。这些SDL2 CMake modules允许您按如下方式构建SDL 2 & 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)

您只需克隆项目中的存储库即可:

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

**注:**如果CMake未找到SDL 2/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

dba5bblo

dba5bblo5#

以下命令对我来说很有效:

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

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

uqdfh47h6#

自2.6版起,SDL2_image安装随附CMake配置脚本SDL2_imageConfig.cmake/SDL2_image-config.cmake
因此,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)

请注意,SDL2_IMAGE_LIBRARIESSDL2_IMAGE_INCLUDE_DIRS等变量在本例中未设置,因此使用它们没有意义。

相关问题