0

What I do want to achieve is separate the compilation and linking steps for multiple .cpp and .h files. Like this: g++ -c *.cpp && g++ -o main *.o. But I would like to do that inside the vscode tasks.json. I tried the following code:

Tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-c",
                "${workspaceFolder}/*.cpp",
                "&& /usr/bin/g++ -o",
                "${fileDirname}/${fileBasenameNoExtension} *.o"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

The error I did receive below:

Console

Starting build...
/usr/bin/g++ -fdiagnostics-color=always -c /home/raijin/Documents/Code/Cpp/test/*.cpp "&& /usr/bin/g++ -o" "/home/raijin/Documents/Code/Cpp/test/main *.o"
g++: warning: && /usr/bin/g++ -o: linker input file unused because linking not done
g++: error: && /usr/bin/g++ -o: linker input file not found: No such file or directory
g++: warning: /home/raijin/Documents/Code/Cpp/test/main *.o: linker input file unused because linking not done
g++: error: /home/raijin/Documents/Code/Cpp/test/main *.o: linker input file not found: No such file or directory

What should I do?

raijin
  • 148
  • 1
  • 8
  • Already tried it but I cannot specify -o with -c, -S or -E – raijin Nov 06 '22 at 15:03
  • the intention is to separate the compilation from linking step, therefore the -c is necessary – raijin Nov 06 '22 at 15:10
  • Sorry I removed the previous comments. I am not sure you can do that as a single task. – drescherjm Nov 06 '22 at 15:16
  • Maybe splitting up to multiple tasks and this: [https://stackoverflow.com/questions/52238171/how-to-run-multiple-tasks-in-vs-code-on-build](https://stackoverflow.com/questions/52238171/how-to-run-multiple-tasks-in-vs-code-on-build) – drescherjm Nov 06 '22 at 15:18
  • 1
    Use vscode integration with a real build system. People are saying [CMake tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools) is good. – n. m. could be an AI Nov 06 '22 at 18:35

1 Answers1

0

Following the comments suggestion and vscode compound tasks doc. Found a way to compile and produce a executable with the code below:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "c++",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-c",
                "${workspaceFolder}/*.cpp",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "custom cpp build",
            "command": "g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "*.o"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
           "dependsOn":["c++"]
        }
    ],
    "version": "2.0.0"
}
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "c++",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-c",
                "${workspaceFolder}/*.cpp",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "custom cpp build",
            "command": "g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "*.o"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
           "dependsOn":["c++"]
        }
    ],
    "version": "2.0.0"
}

Even so it works, it gets quickly complicated and/or unoptimized as a way to build in c++. I'm going to use cmake as suggested from now on.

raijin
  • 148
  • 1
  • 8