Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

build - VSCode c++ task.json include path and libraries

IntelliSense uses c_cpp_properties.json >> includePath to find the headers for auto-completion, but I noticed I still need to specify the include path inside the task.json >> tasks >> args to build.

I found in the documentation that the includePath is pretty much the same path I would specify in "-I":

The paths that you specify for this setting are the same paths that you would send to your compiler via the -I switch. When your source files are parsed, the IntelliSense engine will prepend these paths to the files specified by your #include directives while attempting to resolve them. These paths are not searched recursively.*

link

  1. Am I setting up VSCode correctly by specifying all the libraries and the includes directories inside the args of the build taks ? Or should it be done differently ?
  2. Can someone explain using different words what's the difference between the includePath and browse ? The explanation link is not totally clear to me

Here is an example of my 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
}

and 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"
}

Is a simple question, but I am new to VSCode (sorry).

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1. Am I setting up VSCode correctly?

Mostly. The fact that you have to specify the include paths twice (once in c_cpp_properties.json and again in a file that describes your build) is unavoidable. In VSCode, the build system and the editor do not understand each other, and both need this information. In contrast, with Visual Studio (no "Code"), it would only be necessary to specify the paths once; that is one of the benefits of using a "true" Integrated Development Environment. (But there are drawbacks too; I'm not trying to discourage you from using VSCode.)

However, I do not recommend putting the include paths into tasks.json directly. Rather, one typically has a separate build system that can be invoked from the command line, and then tasks.json invokes that command too.

As a very common example, you could use GNU Make and replace your current tasks.json with this (untested!) Makefile:

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

This tells make how to build test-sdl from main2.cpp, namely by running the g++ command shown. (I have deliberately kept this Makefile very simple since the question isn't about Makefiles; just be aware that a real Makefile would break things up for better organization, and the backslashes are likely to need adjustment.)

In any case, then your tasks.json simplifies to:

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

This is better because you don't have crucial build information locked away in a file that only VSCode understands.

2. Can someone explain ... includePath and browse?

VSCode has two different systems for understanding C++ code. There is the older "Tag Parser", which uses browse.path, and the newer "Intellisense", which uses includePath. At this point (2019-08-30, VSCode 1.37.1), my understanding is basically everyone should be using the newer Intellisense system, as it provides more accurate information and should be at least as mature. Consequently, you should be able to simply ignore browse.path.

To make sure you are using Intellisense rather than Tag Parser, go into File → Preferences → Settings → C/C++ → "C_Cpp: Intelli Sense Engine" and make sure that it is "Default" rather than "Tag Parser". Note that this setting is stored in settings.json rather than c_cpp_properties.json.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...