Search code examples
c++visual-studio-codesdl-2lldb

Setting up SDL2 with OpenGL in Visual Studio Code


I’m trying to set up an OpenGL project using SDL2 in Visual Studio Code on macOS, but I’m running into two issues:

  1. SDL2 Libraries Not Found: Visual Studio Code can’t find the SDL2 libraries even though I’ve installed SDL2. How can I configure it to locate the SDL2 files? It is showing up as red in the library import, and I used to get this error, which I actually already fixed:

    Error: `dyld[3406]: Library not loaded: @rpath/SDL2.framework/Versions/A/SDL2 Referenced from: ... Reason: no LC_RPATH's found`
    
  2. MI Error with LLDB: When I try to debug the project, LLDB gives a machine interface driver error. What’s the correct configuration to fix this?

    Error:

    Starting: "/usr/bin/lldb" --interpreter=mi
    error: unknown option: --interpreter=mi
    Use 'lldb --help' for a complete list of options.
    "/usr/bin/lldb" exited with code 1 (0x1).
    

Here are my configuration files:

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-F/Library/Frameworks",
                "-framework", "SDL2",
                "-framework", "OpenGL",
                "-I/Library/Frameworks/SDL2.framework/Headers",
                "-Wl,-rpath,/Library/Frameworks"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug C++ File",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ build active file",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "miDebuggerPath": "/usr/bin/lldb"
        }
    ]
}

How can I ensure that SDL2 libraries are correctly linked and found by Visual Studio Code? And what configuration changes might be needed to resolve the LLDB debugging issue?


Solution

  • You’re making good progress setting up your OpenGL project with SDL2 in VS Code, but a few issues need addressing.

    1. SDL2 Libraries Not Found: VS Code isn't detecting the SDL2 libraries, causing them to appear with a red error underline. To resolve this, you need to edit the C/C++ configuration UI file. You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette:

    c_cpp_properties.json

    {
        "configurations": [
            {
                "name": "Mac",
                "includePath": [
                    "${workspaceFolder}/**",
                    "/Library/Frameworks/SDL2.framework/Headers"
                ],
                "defines": [],
                "macFrameworkPath": [
                    "/Library/Frameworks"
                ],
                "compilerPath": "/usr/bin/clang",
                "cStandard": "c17",
                "cppStandard": "c++17",
                "intelliSenseMode": "macos-clang-x64"
            }
        ],
        "version": 4
    }
    

    Ensure that the SDL2 library is installed in /Library/Frameworks.

    1. MI Error with LLDB: LLDB is returning an error related to --interpreter=mi, indicating a configuration mismatch or deprecated option. The --interpreter=mi option is no longer part of LLDB. For more details, see lists.llvm.org. As a workaround, use the following configuration:

    launch.json

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Launch C++ Program",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/${fileBasenameNoExtension}",
                "args": [],
                "cwd": "${workspaceFolder}",
                "preLaunchTask": "C/C++: clang++ build active file",
                "stopAtEntry": false,
                "MIMode": "lldb"
            }
        ]
    }
    

    Ensure you also have the C/C++ VS Code extension installed!