【问题标题】:SDL2 and CMake on OS X 10.11OS X 10.11 上的 SDL2 和 CMake
【发布时间】:2016-04-19 11:31:47
【问题描述】:

我知道有一些答案(例如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 上 downloadedSDL2 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

如果我在 cmake 子目录中包含 FindSDL2.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?

【问题讨论】:

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


    【解决方案1】:

    您需要为您的目标添加target_link_libraries(YOUR_TARGET ${SDL2_LIBRARY}) - 它没有链接到 sdl。

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

    【讨论】:

    • 已修复。逗号、引号、分号……我的 cmake 很糟糕
    猜你喜欢
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2016-01-21
    • 2016-05-10
    相关资源
    最近更新 更多