C++ VSCode c++;json包括路径和库

C++ VSCode c++;json包括路径和库,c++,build,visual-studio-code,g++,C++,Build,Visual Studio Code,G++,IntelliSense使用c_cpp_properties.json>>includePath来查找自动完成的标题,但我注意到我仍然需要在task.json>>tasks>>args中指定要构建的include路径 我在文档中发现includePath与我在“-I”中指定的路径几乎相同: 为此设置指定的路径与 您将通过-I开关发送到编译器。当你的消息来源 当解析文件时,IntelliSense引擎会将这些路径前置到 您的#指定的文件在尝试 解决它们。这些路径不是递归搜索的* 我是否通过在bu

IntelliSense使用c_cpp_properties.json>>includePath来查找自动完成的标题,但我注意到我仍然需要在task.json>>tasks>>args中指定要构建的include路径

我在文档中发现includePath与我在“-I”中指定的路径几乎相同:

为此设置指定的路径与 您将通过-I开关发送到编译器。当你的消息来源 当解析文件时,IntelliSense引擎会将这些路径前置到 您的#指定的文件在尝试 解决它们。这些路径不是递归搜索的*

  • 我是否通过在build tak的args中指定所有库和includes目录来正确设置VSCode?还是应该采取不同的做法
  • 有人能用不同的词解释一下includePath和browse之间的区别吗?解释链接对我来说并不完全清楚
  • 以下是我的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
    }
    
    和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"
    }
    

    这是一个简单的问题,但我不熟悉VSCode(对不起)。

    我也不熟悉VS代码。此外,我从未建立过更大的C++项目。至少现在,我是这样解决这座大楼的。(见下文)

    我在tasks.json中完成了这项工作

    (似乎您无法逃脱args属性中的空白,)

    我删除了args属性,并将command属性设置为运行一个脚本(buildmysorry.sh),该脚本只执行以下操作

    #!/bin/bash
    
    g++ fullpathtodir/hello.cpp -Wall -g $(sdl2-config --cflags --libs) -o fullpathtodir/hello
    
    将fullpathtodir替换为您的路径。

    1。我是否正确设置VSCode? 大部分。您必须两次指定include路径(一次在
    c\u cpp\u properties.json中,一次在描述构建的文件中),这是不可避免的。在VSCode中,构建系统和编辑器彼此不理解,并且都需要这些信息。相反,对于VisualStudio(无“代码”),只需要指定一次路径;这是使用“真正的”集成开发环境的好处之一。(但也有缺点;我并不是想阻止您使用VSCode。)

    但是,我不建议直接将include路径放入
    tasks.json
    。相反,通常有一个单独的构建系统,可以从命令行调用,然后
    tasks.json
    也调用该命令

    作为一个非常常见的示例,您可以使用以下(未测试的!)Makefile使用并替换当前的
    tasks.json

    test-sdl: main2.cpp
        g++ -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
    
    这将告诉
    make
    如何从
    main2.cpp
    构建
    test sdl
    ,即运行所示的
    g++
    命令。(我故意让这个Makefile非常简单,因为问题不是关于Makefiles;请注意,一个真正的Makefile会为了更好的组织而将事情分解,并且反斜杠可能需要调整。)

    在任何情况下,您的
    tasks.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": "make",   // <-- changed
                "args": []           // <-- changed
            }
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "problemMatcher":"$gcc"
    }
    
    {
    //看https://go.microsoft.com/fwlink/?LinkId=733558
    //有关tasks.json格式的文档
    “版本”:“2.0.0”,
    “任务”:[
    {
    “标签”:“构建”,
    “类型”:“外壳”,
    
    “command”:“make”,//我也尝试过使用库,至少目前可以使用(我使用的是Windows BTW): 在c_cpp_properties.json中,我引用了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
    }
    
    在tasks.json中,我有一个编译和链接器任务,以及一个同时运行这两个任务的任务:

    {
        // 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
                }
            }
        ]
    }
    

    这就是我在Mac上的VS代码(MacBook Pro 2015,macOS Catalina)中加入OpenGL“GLWF”和“glad”库的方式。我有intellisense,可以构建和调试

    include/glad/glad.h
    -要包括的库文件

    src/helloworld.cpp-主文件

    /* 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/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(您需要包括实现文件
    include/glad.c
    ,而不仅仅是标题)


    我也有同样的情况,问同样的问题。如果你找到了解释,请分享,谢谢:)我尝试了这种方法,它似乎很有效。我唯一的问题是,如果你在上一个任务中声明所有依赖项,它们将并行执行,因此有时你会链接以前版本中的.o文件。相反,我建议只在上一个任务中声明“链接器”依赖项,在“链接器”任务中声明“编译器”依赖项。这样,链接器将等待编译器实际完成作业。是的,@FrancescoRogo,说得好。
    {
      "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
    }
    
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "(lldb) Launch",
                "type": "lldb",
                "request": "launch",
                "program": "${workspaceFolder}/build/helloworld.out",
                "args": [],
                "cwd": "${workspaceFolder}"
            }
        ]
    }
    
    {
        "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
                }
            }
        ]
    }