6

I'm following a couple of tutorials from reddit.com/r/limeoats to learn some game development in c++. I'm not experienced with CMake or CLion and have managed to google my way this far.

I had everything working up until I updated OSX to El Capitan (10.11). It seems that I can no longer use #include "SDL2/SDL.h" but instead need to use #include "SDL.h" Then it can find the SDL headers. The problem comes in when I also use #include "SDL_image.h" I get the following compiler error:

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

Looking into the header file in my Frameworks folder, it has #include <SDL2/SDL.h> but CMake is providing it as SDL.h for some reason after upgrading OSX to 10.11.

How do I get the SDL extensions to play nice with the updated header path? Or how do I get CMake to give me the old SDL2/SDL.h header path back?

Below is my CMakeLists.txt and I got the FindSDL2.cmake (note the comment on line 50) and FindSDL2_image.cmake from here.

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

And my directory structure (if it helps)...

/Cavestory (root)
    CMakeLists.txt
    /bin
    /cmake
        FindSDL2.cmake
        FindSDL2_image.cmake
    /content
        /sprites
            **images**
    /docs
    /source
        /headers
           **header files**
        /src
           **code files**
7
  • Try to remove include_directories(${SDL2_INCLUDE_DIR}) include_directories(${SDL2_IMAGE_INCLUDE_DIR}) because it is redundant. find_package will do it for you.
    – MinaKamel
    Oct 14, 2015 at 21:32
  • As an aside, you should include SDL.h, not SDL/SDL.h. Source.
    – Emil Laine
    Oct 14, 2015 at 21:36
  • @MinaKamel - Removed those lines, the compiler cannot find sdl.h nor SDL2/sdl.h - based on what i've read FindSDL.cmake sets those variables but does not call include_directories with them.
    – Robin
    Oct 14, 2015 at 21:42
  • @zenith - that is what i'm doing and it works in my code but does not work for the SDL_image.h (which is not mine, and includes <SDL2/sdl.h> as I tried to state above.
    – Robin
    Oct 14, 2015 at 21:44
  • It seems that current SDL2_image is incompatible with newest SDL2. Set SDL2DIR environment variable (note $ENV{SDL2DIR} in the findSDL2.cmake) to old SDL2 installation and make fresh configuration.
    – Tsyvarev
    Oct 14, 2015 at 21:56

1 Answer 1

3

I feel stupid... My CMakeLists.txt file is correct except i needed to add the following

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

instead of

target_link_libraries(Cavestory SDL2 SDL2_image)

This fixes the SDL linking issue from SDL_image.h; However, after updating to El Capitan, I can no longer reference SDL via #include "SDL2/SDL.h" and must use #include "SDL.h" -- while this is preferred, I'd like to know why/how this changed between OSX 10.10 and OSX 10.11.

3
  • Changing target_link_libraries cannot affect on include directories(linking comes after compiling sources). Probably, problem was in something else. In any case it is good that you have resolved it.
    – Tsyvarev
    Oct 15, 2015 at 6:59
  • @Rob Is there anything else you've tried that's outside this thread? I saw the same behavior after upgrading to El Capitan, and followed this thread with no success. Oct 17, 2015 at 6:30
  • I did not. I am including SDL in my code via #include "SDL.h" instead of #include "SDL/SDL.h" not sure why it changed between OSX versions, which is weird to me.
    – Robin
    Oct 19, 2015 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.