muffel
muffel

Reputation: 7360

SDL2 and CMake on OS X 10.11

I know there are some answers (e.g. here, here or here), but I can't get it to work:

Consider a MWE main.cpp

// depending on the source of the example,
// one of the two includes is used
//#include <SDL2/SDL.h>
#include "SDL.h"

int main() {
    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );
    //Quit SDL
    SDL_Quit();
    return 0;
}

I downloaded SDL2 v2.0.4 on OS X 10.11 and copied the SDL2.framework file into the /Library/Frameworks folder.

Now I am trying to find a working CMake script to compile main.cpp.

First i tried

find_package(SDL)
add_executable(example main.cpp)
target_include_directories(example ${SDL_INCLUDE_DIR})
target_link_libraries(example ${SDL_LIBRARY})

But I get the error message

Could NOT find SDL (missing:  SDL_LIBRARY) (found version "2.0.4")
CMake Error at CMakeLists.txt:3 (target_include_directories):
  target_include_directories called with invalid arguments

If I included FindSDL2.cmake in a cmake subdirectory and use the script

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(example main.cpp)

CMake works, but I get a linker error:

Undefined symbols for architecture x86_64:
  "_SDL_Init", referenced from:
      _main in main.cpp.o
  "_SDL_Quit", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Do you have any idea what my problems are and how I can use SDL2 properly this way?

Upvotes: 2

Views: 6125

Answers (1)

xaxxon
xaxxon

Reputation: 19751

you need to add target_link_libraries(YOUR_TARGET ${SDL2_LIBRARY}) for your target - it's not linking against sdl.

If you run make VERBOSE=1 you can see the actual link.

Upvotes: 3

Related Questions