sdl2/sdl.h 文件未找到

问题描述 投票:0回答:1

当我尝试在 mac 上使用 vscode 和 clang++ 创建一个窗口时,我遇到了一个问题,其中安装了 sdl2(使用 home-brew)但 vscode 找不到它。这是我在终端中遇到的错误。

 *  Executing task: C/C++: clang build active file 

Starting build...
/usr/bin/clang -std=gnu++14 -std=c++17 -stdlib=libc++ -g 
/Users/jst_/Coding/VS.Code_C++/Main.CPP -o 
/Users/jst_/Coding/VS.Code_C++/Main
/Users/jst_/Coding/VS.Code_C++/Main.CPP:1:10: fatal error: 'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>
         ^~~~~~~~~~~~
1 error generated.

Build finished with error(s).

 *  The terminal process failed to launch (exit code: -1). 
 *  Terminal will be reused by tasks, press any key to close it. 

The code for the window is


#include <SDL2/SDL.h>

int main() {
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window* window = SDL_CreateWindow("My Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    if (!window) {
        // Handle window creation failure
        SDL_Quit();
        return 1;
    }

    SDL_Delay(3000); // Pause for 3 seconds

    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

任务 json 是

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-std=c++17",
                "-stdlib=libc++",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-stdlib=libc++",
                "-g",
                "${file}",
                "-I",
                "/opt/homebrew/opt/sdl2", // Replace this path with the correct one
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

C_cpp_properties json 是

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/homebrew/opt/sdl2",
                "/opt/homebrew/Cellar/sdl2/2.28.5/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

我多次尝试寻找不同的路径,最终找到了正确的路径,但没有什么区别,我仍然遇到相同的错误。

c++ c++17 sdl-2
1个回答
0
投票

在使用 clang 编译的 Tasks.json 中的第一个任务中,您忘记添加 args 的路径,这就是您收到错误的原因,因为您使用 clang 进行编译,并且在该任务中包含路径(其中 SDL/sdl2 .h 位于)丢失。

© www.soinside.com 2019 - 2024. All rights reserved.