MAC - SDL2 Build Error - Undefined symbols for architecture x86_64


SDL2 Link

Install SDL2


DMG : Copy the SDL2.framework to/Library/Frameworks
Source Code :/Floder/SDL2-2.0.5/docs/README-macosx.md

My Project CMakeList.txt file:

cmake_minimum_required(VERSION 3.6)
project(Simple_ffmpeg_player)

set(CMAKE_CXX_STANDARD 11)

include_directories(/usr/local/Cellar/ffmpeg/3.1.4/include/)
link_directories(/usr/local/Cellar/ffmpeg/3.1.4/lib/)

set(SOURCE_FILES main.cpp)

add_executable(Simple_ffmpeg_player ${SOURCE_FILES})

target_link_libraries(
        Simple_ffmpeg_player
        avcodec
        avdevice
        avfilter
        avformat
        avresample
        avutil
        postproc
        swresample
        swscale
)

Build Error Info :

4 warnings generated.
[100%] Linking CXX executable Simple_ffmpeg_player
Undefined symbols for architecture x86_64:
  "_SDL_CreateRenderer", referenced from:
      _main in main.cpp.o
  "_SDL_CreateTexture", referenced from:
      _main in main.cpp.o
  "_SDL_CreateWindow", referenced from:
      _main in main.cpp.o
  "_SDL_Delay", referenced from:
      _main in main.cpp.o
  "_SDL_GetError", referenced from:
      _main in main.cpp.o
  "_SDL_Init", referenced from:
      _main in main.cpp.o
  "_SDL_Quit", referenced from:
      _main in main.cpp.o
  "_SDL_RenderClear", referenced from:
      _main in main.cpp.o
  "_SDL_RenderCopy", referenced from:
      _main in main.cpp.o
  "_SDL_RenderPresent", referenced from:
      _main in main.cpp.o
  "_SDL_UpdateTexture", referenced from:
      _main in main.cpp.o
  "_SDL_UpdateYUVTexture", 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)
make[3]: *** [Simple_ffmpeg_player] Error 1
make[2]: *** [CMakeFiles/Simple_ffmpeg_player.dir/all] Error 2
make[1]: *** [CMakeFiles/Simple_ffmpeg_player.dir/rule] Error 2
make: *** [Simple_ffmpeg_player] Error 2

Soultion 1:

cmake_minimum_required(VERSION 3.6)
project(Simple_ffmpeg_player)

set(CMAKE_CXX_STANDARD 11)

include_directories(/usr/local/Cellar/ffmpeg/3.1.4/include/)
link_directories(/usr/local/Cellar/ffmpeg/3.1.4/lib/)

include_directories(/usr/local/Cellar/sdl2/2.0.5/include/)
link_directories(/usr/local/Cellar/sdl2/2.0.5/lib/)

set(SOURCE_FILES main.cpp)

add_executable(Simple_ffmpeg_player ${SOURCE_FILES})

target_link_libraries(
        Simple_ffmpeg_player
        avcodec
        avdevice
        avfilter
        avformat
        avresample
        avutil
        postproc
        swresample
        swscale
        ${SDL2_LIBRARY} # Important
)

Soultion 2


http://stackoverflow.com/questions/36717135/sdl2-and-cmake-on-os-x-10-11
https://github.com/brendan-w/collector/blob/master/cmake/FindSDL2.cmake#L50
cmake_minimum_required(VERSION 3.6)
project(Simple_ffmpeg_player)

set(CMAKE_CXX_STANDARD 11)

include_directories(/usr/local/Cellar/ffmpeg/3.1.4/include/)
link_directories(/usr/local/Cellar/ffmpeg/3.1.4/lib/)

include_directories(/usr/local/Cellar/sdl2/2.0.5/include/)
link_directories(/usr/local/Cellar/sdl2/2.0.5/lib/)

set(SOURCE_FILES main.cpp)

# Important
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
find_package(SDL2 REQUIRED)

include_directories(${SDL2_INCLUDE_DIR})
link_directories(${SDL2_LIBRARY})

add_executable(Simple_ffmpeg_player ${SOURCE_FILES})

target_link_libraries(
        Simple_ffmpeg_player
        avcodec
        avdevice
        avfilter
        avformat
        avresample
        avutil
        postproc
        swresample
        swscale
        ${SDL2_LIBRARY} #Important
)