OS X 10.11 上的 SDL2 和 CMake

SDL2 and CMake on OS X 10.11

我知道有一些答案(例如 here, here or ),但我无法让它工作:

考虑一个 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 并将 SDL2.framework 文件复制到 /Library/Frameworks文件夹。

现在我正试图找到一个工作的 CMake 脚本来编译 main.cpp.

首先我尝试了

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

但我收到错误消息

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

如果我将 FindSDL2.cmake 包含在 cmake 子目录中并使用脚本

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 工作,但我收到链接器错误:

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)

您是否知道我的问题是什么以及我如何才能以这种方式正确使用 SDL2?

您需要为您的目标添加 target_link_libraries(YOUR_TARGET ${SDL2_LIBRARY}) - 它不是 linking against sdl。

如果你 运行 make VERBOSE=1 你可以看到实际的 link。