You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arm64架构Mac(M1 Pro)下VS Code运行SDL2遇未定义符号错误

解决Apple M1 Pro上VS Code编译SDL2的链接错误

问题场景

在Apple M1 Pro芯片的Mac上,已配置SDL2并尝试运行以下C++示例代码:

// On linux compile with:
// g++ -std=c++17 main.cpp -o prog -lSDL2

// C++ Standard Libraries
#include <iostream>

// Third-party library
#include <SDL2/SDL.h>

int main(int argc, char* argv[]){

    // Create a window data type
    // This pointer will point to the 
    // window that is allocated from SDL_CreateWindow
    SDL_Window* window=nullptr;

    // Initialize the video subsystem.
    // If it returns less than 1, then an
    // error code will be received.
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        std::cout << "SDL could not be initialized: " <<
                  SDL_GetError();
    }else{
        std::cout << "SDL video system is ready to go\n";
    }

    // Request a window to be created for our platform
    // The parameters are for the title, x and y position,
    // and the width and height of the window.
    window = SDL_CreateWindow("C++ SDL2 Window",
            0,
            2500,
            640,
            480,
            SDL_WINDOW_SHOWN);

    // We add a delay in order to see that our window
    // has successfully popped up.
    SDL_Delay(3000);

    // We destroy our window. We are passing in the pointer
    // that points to the memory allocated by the 
    // 'SDL_CreateWindow' function. Remember, this is
    // a 'C-style' API, we don't have destructors.
    SDL_DestroyWindow(window);
    
    // We safely uninitialize SDL2, that is, we are
    // taking down the subsystems here before we exit
    // our program.
    SDL_Quit();
    return 0;
}

编译错误信息

编译时出现链接错误,提示找不到arm64架构的SDL相关符号:

Undefined symbols for architecture arm64:
  "_SDL_CreateWindow", referenced from:
      _main in gui-67ced6.o
  "_SDL_Delay", referenced from:
      _main in gui-67ced6.o
  "_SDL_DestroyWindow", referenced from:
      _main in gui-67ced6.o
  "_SDL_GetError", referenced from:
      _main in gui-67ced6.o
  "_SDL_Init", referenced from:
      _main in gui-67ced6.o
  "_SDL_Quit", referenced from:
      _main in gui-67ced6.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

VS Code适配的解决办法

1. 确认SDL2安装路径(Homebrew方式)

如果是通过Homebrew安装SDL2,M1 Mac上的默认路径为:

  • 头文件:/opt/homebrew/include/SDL2
  • 库文件:/opt/homebrew/lib
    若尚未安装,执行以下命令安装:
brew install sdl2

2. 配置VS Code编译任务(tasks.json)

  • 打开VS Code,按下Ctrl+Shift+B,选择「创建tasks.json文件」,再选择「C/C++: clang++ 生成活动文件」
  • 打开生成的.vscode/tasks.json,修改args数组,添加SDL2的编译链接参数,最终配置示例:
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ 生成活动文件",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-I/opt/homebrew/include/SDL2",
                "-L/opt/homebrew/lib",
                "-lSDL2",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译器: /usr/bin/clang++"
        }
    ]
}
  • 保存配置后,再次按下Ctrl+Shift+B执行编译即可。

3. 手动编译命令

若不想配置tasks.json,可直接在终端执行以下命令编译:

clang++ -std=c++17 main.cpp -o prog -I/opt/homebrew/include/SDL2 -L/opt/homebrew/lib -lSDL2

编译完成后运行程序:

./prog

内容的提问来源于stack exchange,提问作者Anirudh Mathur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 03:00:00