Home > Net >  Create task that builds cpp and c files in Visual Studio Code
Create task that builds cpp and c files in Visual Studio Code

Time:05-06

I have task in my task.json file in Visual Studio Code that builds project :

"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C  : build",
        "command": "/usr/bin/g  ",
        "args": [
            "-g",
            "${workspaceFolder}/*.cpp",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}",
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "compiler: /usr/bin/g  "
    },

Task is called from launch.json. Does line "${workspaceFolder}/*.cpp" means that task builds only cpp files? How to deal if project has both - cpp and c files?

CodePudding user response:

G can compile any .c or .cpp files but they will be treated as C files only.
GCC can compile any .c or .cpp files but they will be treated as C and C respectively.

compilerArgs (optional) Compiler arguments to modify the includes or defines used, for example -nostdinc , -m32, etc.

You could try using GCC.

Else if you have a main.cpp the uses

#include "example.h"
#include "example2.h"

compiling only the main file should work.

Perhaps this might bring some more clarification : https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

CodePudding user response:

"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C  : build",
        "command": "/usr/bin/gcc",
        "args": [
            "-g",
            "${workspaceFolder}/*.cpp",
            "${workspaceFolder}/*.c", // this should build c and cpp at the same time.
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}",
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "compiler: /usr/bin/gcc"
    },
  • Related