1
votes

I'm using VSCode to create a C++ project and keep getting this error when trying to build && debug. When running from console if I use 'g++ -o main 'main.cpp' 'file_1.cpp' 'file_2.cpp'' it works and compiles correctly.

I have read that this is something to do with the linking of files? Does anyone know how to fix this in VSCode? I have a default launch.json configuration file that builds the active file if this helps.

Here is the contents of my tasks.json file:

{
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "shell: g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "/usr/bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    },
    {
        "type": "shell",
        "label": "g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "/usr/bin"
        }
    }
]

}

EDIT: I'm using Debian 10 'Buster'

Many thanks

1
I looked into the c/c++ configuration and there's an option to add compiler arguments but I can't seem to find any way of specifying the files (ideally it'd need to recursively search the workspace for all .cpp files) - Jr795
This should be in the .json files. - drescherjm
@drescherjm I'm using g++ to compile on Linux - Jr795
You can list the files in there, just like you did on the command line. You might also want to think about using something like CMake to not have to make the changes inside your tasks.json file all the time - UnholySheep

1 Answers

1
votes

You need to modify tasks.json for compile all .cpp . In args change ${file} for ${workspaceFolder}/*.cpp To run the build task press Ctrl+Shift+B, this generate a execution file in the current directory. Execute this file in the terminal for run the program (./fileName)