Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode c++ task.json include path and libraries

Tags:

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).

like image 277
Diego Trazzi Avatar asked Oct 20 '18 21:10

Diego Trazzi


2 Answers

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.

like image 164
Scott McPeak Avatar answered Sep 17 '22 13:09

Scott McPeak


I too have attempted to use libraries, and at least for now this works (I'm on Windows BTW): In c_cpp_properties.json, I have a reference to the include directory:

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

And in tasks.json, I have A Compilation and a Linker task, and a task that runs both:

{     // 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             }         }     ] } 
like image 37
MemzIcon Avatar answered Sep 19 '22 13:09

MemzIcon