在OSX 10.11上使用带有SDL2_IMAGE的SDL2(Clion 1.1中的CMake 3.3)

2022-08-21 00:00:00 macos sdl-2 cmake c++ clion

我正在学习来自reddit.com/r/limeoats的几个教程,以学习一些用C++开发的游戏。我没有使用CMake或Clion的经验,到目前为止我还是设法用谷歌搜索了一下。

在我将OSX更新到El Capitan(10.11)之前,一切都在进行中。似乎我不能再使用#include "SDL2/SDL.h",而是需要使用#include "SDL.h",这样它才能找到SDL标头。当我还使用#include "SDL_image.h"时出现问题,我得到以下编译器错误:

/Library/Frameworks/SDL2_image.framework/Headers/SDL_image.h:27:10:
fatal error: 'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>
         ^

查看My Frameworks文件夹中的头文件,它有#include <SDL2/SDL.h>,但CMake在将OSX升级到10.11后出于某种原因将其作为SDL.h提供。

如何让SDL扩展与更新的头路径配合得很好?或者如何让CMake将旧的SDL2/SDL.h头路径返回给我?

下面是我的CMakeLists.txt,我从here获得了FindSDL2.cmake(请注意第50行的注释)和FindSDL2_Image.cmake。

cmake_minimum_required(VERSION 3.3)
project(Cavestory)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

include_directories(${PROJECT_SOURCE_DIR}/source/headers)

find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED >=2.0.0)

include_directories(${SDL2_INCLUDE_DIR})
include_directories(${SDL2_IMAGE_INCLUDE_DIR})

file(GLOB SOURCE_FILES "source/src/*.cpp")

add_executable(Cavestory ${SOURCE_FILES})

# One thread said this is all I should need to link SDL2
# but cannot get this to work at all
#target_link_libraries(Cavestory SDL2 SDL2_image)

#add_custom_command(TARGET Cavestory POST_BUILD
#        COMMAND ${CMAKE_COMMAND} -E copy_directory
#        ${CMAKE_SOURCE_DIR}/content $<TARGET_FILE_DIR:Cavestory>)

和我的目录结构(如果有帮助)...

/Cavestory (root)
    CMakeLists.txt
    /bin
    /cmake
        FindSDL2.cmake
        FindSDL2_image.cmake
    /content
        /sprites
            **images**
    /docs
    /source
        /headers
           **header files**
        /src
           **code files**

解决方案

我觉得自己很傻...我的CMakeLists.txt文件是正确的,但我需要添加以下内容

target_link_libraries(Cavestory ${SDL2_LIBRARY} ${SDL2_IMAGE_LIBRARY})

而不是

target_link_libraries(Cavestory SDL2 SDL2_image)

这修复了sdl_Image.h中的SDL链接问题;但是,在更新到El Capitan后,我不能再通过#include "SDL2/SDL.h"引用SDL,而必须使用#include "SDL.h"--虽然这是首选方法,但我想知道为什么/如何在OSX 10.10和OSX 10.11之间发生变化。

相关文章