如何在cmake中使用SDL2和SDL_image

时间:2022-10-23 12:05:37

I'm looking for the simplest way to compile a c++ program using SDL2 and SDL_image with cmake.

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

Here is my best attempt, after hours of searching:

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

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! :)

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

3 个解决方案

#1


30  

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的第三个参数。

#2


3  

There are two blog posts about this here:

这里有两篇博客文章:

Using SDL2 with CMake

将SDL2与CMake一起使用

Using SDL2_image with CMake

在CMake中使用SDL2_image

Basically you need a FindSDL2.cmake and FindSDL2_image.cmake module. 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.cmake和FindSDL2_image.cmake模块。它们可以基于适用于SDL 1.2的那些,它们已经包含在CMake中。使用这些查找模块也适用于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})

#3


1  

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上开箱即用。

#1


30  

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的第三个参数。

#2


3  

There are two blog posts about this here:

这里有两篇博客文章:

Using SDL2 with CMake

将SDL2与CMake一起使用

Using SDL2_image with CMake

在CMake中使用SDL2_image

Basically you need a FindSDL2.cmake and FindSDL2_image.cmake module. 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.cmake和FindSDL2_image.cmake模块。它们可以基于适用于SDL 1.2的那些,它们已经包含在CMake中。使用这些查找模块也适用于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})

#3


1  

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上开箱即用。