使用自制软件安装 SDL2 Xcode

Using homebrew installed SDL2 with Xcode

我已经使用 Homebrew 安装了 SDL2,但现在我不知道如何确保 Xcode 可以使用它!我导入了创建的库并将其添加到项目的构建阶段选项卡中。但是当我尝试构建时出现错误 'SDL2/SDL.h' not found

为了能够在 Xcode 上使用 SDL2,您必须设置两项(通常 SDL 需要):

  • 在哪里可以找到头文件(以便 Clang 可以用 -Iheader/path 编译)
  • 在哪里可以找到 .dylib 以 link 它到项目中(因为使用 brew 你没有真正的 .framework

要知道正确的路径,您应该调用 sdl2-config --cflagssdl2-config --libs。在我的系统上这些产生:

:~jack$ /usr/local/bin/sdl2-config --cflags
-I/usr/local/include/SDL2 -I/usr/X11R6/include -D_THREAD_SAFE

:~jack$ /usr/local/bin/sdl2-config --libs
-L/usr/local/lib -lSDL2

现在只需将第一个粘贴到项目的 other C flags 中,将另一个粘贴到项目的 other linker flags 字段中,就可以开始了。

您可以在正确的字段中设置它们,-IHeader Search Paths-lLibrary Search Path,但结果是一样的。

  1. brew 搜索 sdl2

  2. brew install sdl2 sdl2_image sdl2_mixer sdl2_net sdl2_ttf

  3. config xcode 构建设置 --> 所有 --> 搜索路径 --> Header 搜索路径
    --> /usr/local/include

  4. config Xcode 常规 -> 添加框架和库 --> libSDL2-2.0.0.dylib

  5. 测试你的代码

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

using namespace std;

int main() {

    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
        cout << "SDL init failed." << endl;
        return 1;
    }

    cout << "SDL Init succeeded." << endl;

    SDL_Quit();

    return 0;
}
  1. https://brew.sh

    安装已安装的自制软件
  2. 输入终端brew install sdl2

  3. 然后显示框架的路径(在xCode select项目文件>>构建设置>>header搜索路径)并使用cmd+shift+ g 类型 /usr/local/include

  4. 在'General''Frameworks & Libraries'中放入libSDL2-2.0.0.dylib(这里是/usr/local/Cellar/sdl2/2.0.14_1/lib)

  5. 最重要的检查 'Disable Library Validations' 在 'Signing & Capabilities'

完成这些步骤后代码开始为我工作。