web-dev-qa-db-fra.com

VSCode c ++ task.json inclut le chemin et les bibliothèques

IntelliSense utilise c_cpp_properties.json >> includePath pour trouver les en-têtes pour la saisie semi-automatique, mais j'ai remarqué que je dois toujours spécifier le chemin d'inclusion à l'intérieur des arguments task.json >> tasks >> pour construire.

J'ai trouvé dans la documentation que le includePath est à peu près le même chemin que je spécifierais dans "-I":

Les chemins que vous spécifiez pour ce paramètre sont les mêmes que ceux que vous enverriez à votre compilateur via le commutateur -I. Lorsque vos fichiers source sont analysés, le moteur IntelliSense ajoute ces chemins aux fichiers spécifiés par vos directives #include tout en essayant de les résoudre. Ces chemins ne sont pas recherchés récursivement. *

lien

  1. Suis-je en train de configurer VSCode correctement en spécifiant toutes les bibliothèques et les répertoires includes dans les arguments des taks de build? Ou faut-il procéder différemment?
  2. Quelqu'un peut-il expliquer en utilisant des mots différents quelle est la différence entre includePath et parcourir? Le lien d'explication n'est pas totalement clair pour moi

Voici un exemple de mon c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/github/dependencies/SDL2-2.0.8/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}/**"
                ]
            }
        }
    ],
    "version": 4
}

et task.json:

{
    // See https://go.Microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "Shell",
            "command": "g++",
            "args": [
                "-g",
                "main2.cpp",
                "-ID:\\github\\dependencies\\SDL2-2.0.8\\include",
                "-LD:\\github\\dependencies\\SDL2-2.0.8\\lib\\x64",
                "-lSDL2main","-lSDL2", "-lopengl32",
                "-o",
                "test-sdl"
            ]
        }
    ],
    "group": {
        "kind": "build",
        "isDefault": true
    },
    "problemMatcher":"$gcc"
}

Est une question simple, mais je suis nouveau sur VSCode (désolé).

7
Diego Trazzi

Moi aussi, j'ai essayé d'utiliser des bibliothèques, et au moins pour l'instant cela fonctionne (je suis sur Windows BTW): Dans c_cpp_properties.json, j'ai une référence au répertoire include:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\ProgrammingLibraries\\SDL2-2.0.10\\include\\SDL2",
                "${workspaceFolder}\\src\\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

Et dans tasks.json, j'ai une tâche de compilation et de liaison, et une tâche qui s'exécute à la fois:

{
    // See https://go.Microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compiler",
            "type": "Shell",
            "command": "g++",
            "args": [
                "-c",
                "${workspaceFolder}\\src\\main.cpp",
                "-IC:\\ProgrammingLibraries\\SDL2-2.0.10\\include\\SDL2"
            ]
        },
        {
            "label": "Linker",
            "type": "Shell",
            "command": "g++",
            "args": [
                "${workspaceFolder}\\main.o",
                "-o",
                "${workspaceFolder}\\bin\\HelloSDL.exe",
                "-LC:\\ProgrammingLibraries\\SDL2-2.0.10\\lib",
                "-lmingw32",
                "-lSDL2main",
                "-lSDL2"
            ]
        },
        {
            "label": "Build HelloSDL",
            "dependsOn": [
                "Compiler",
                "Linker"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
0
MemzIcon

C'est ainsi que j'ai inclus les bibliothèques OpenGL "GLWF" et "happy" dans VS Code sur Mac (MacBook Pro 2015, macOS Catalina) avec clang. Et j'ai intellisense, je peux construire et déboguer.

include/glad/glad.h - fichier de bibliothèque à inclure

src/helloworld.cpp - fichier principal

/* Ask for an OpenGL Core Context */
#define GLFW_INCLUDE_GLCOREARB
#include <GLFW/glfw3.h>
#include <glad/glad.h>

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

int main(int argc, char **argv)
{
    GLFWwindow *window;

    /* Initialize the library */
    if (!glfwInit())
    {
        return -1;
    }

#ifdef __Apple__
    /* We need to explicitly ask for a 3.2 context on OS X */
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(1280, 720, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

.vscode/c_cpp_properties.json

{
  "configurations": [
    {
      "name": "Mac",
      "includePath": ["${workspaceFolder}/src/", "${workspaceFolder}/include/"],
      "defines": [],
      "macFrameworkPath": [
        "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
      ],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "${default}",
      "browse": {
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
      }
    }
  ],
  "version": 4
}

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/build/helloworld.out",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

.vscode/tasks.json (vous devez inclure le fichier d'implémentation include/glad.c, pas seulement les en-têtes)

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build with Clang",
            "type": "Shell",
            "command": "clang++",
            "args": [
                "-std=c++17",
                "-stdlib=libc++",
                "-lglfw3",
                "--include-directory=include/",
                "--include=include/glad.c",
                "-framework",
                "OpenGL",
                "-framework",
                "IOKit",
                "-framework",
                "Cocoa",
                "src/helloworld.cpp",
                "-o",
                "build/helloworld.out",
                "--debug"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
0
Michael Klishevich