OS X 10.11上的SDL2和CMake

时间:2016-04-19 11:31:48

标签: c++ macos cmake sdl sdl-2

我知道有一些答案(例如hereherehere),但我无法解决问题:

考虑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;
}

我在OS X 10.11上downloaded SDL2 v2.0.4并将 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?

1 个答案:

答案 0 :(得分:1)

您需要为目标添加target_link_libraries(YOUR_TARGET ${SDL2_LIBRARY}) - 它不会与sdl相关联。

如果您运行make VERBOSE=1,则可以看到实际链接。

相关问题