0

So I've been learning C++ and all my practice was done in vs code so far. However, when I got to the OOP stuff I couldn't compile multiple files all at once with the extension. So, I googled how to do it and everywhere I went I found it can be done with tasks. It took me hours to find the right code for the tasks.json file that actually works. However, it can only compile and I manually have to run the .exe file. Obviously I wanna do both but I have no idea how. I just need the extra bit that allows the task to run the file after it is compiled. I looked up the documentation for tasks with vs code, but it is very confusing and makes no sense to me at all. I wanna learn C++ not how to do tasks.json lol. Hopefully, someone can help me. If you are gonna tell me to look up the documentation or something don't say anything at all because I am not posting this to have people tell me look up the documentation because that documentation is garbage.

Here is the current tasks.json that can only compile:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C++ Compile",
            "command": "C:\\Program Files\\mingw-w64\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${fileDirname}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Thank you for your time, and any help is appreciated.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    You may want to move up to [Visual Studio Community](https://visualstudio.microsoft.com/vs/community/) which is a lot better for multi-source file projects. – tadman Sep 09 '20 at 22:43
  • Instead of learning about tasks.json (which [this question](https://stackoverflow.com/questions/30269449/how-do-i-set-up-visual-studio-code-to-compile-c-code) seems to have a lot of), invest in learning about generally usable build systems such as Makefiles or CMake. I believe VSC has a passable CMake plugin, or you can just generate your project from the terminal and then have VSC call `cmake --build ${BUILDDIR}` – Botje Sep 09 '20 at 22:44
  • 2
    I'm with tadman. VS Code can be a problem for folks unfamiliar with how it works and are learning to program at the same time. You'll have trouble figuring out if you've made a coding error or if you've mis-configured VS Code. You probably don't want to have to learn two things at the same time. – user4581301 Sep 09 '20 at 22:49
  • You might want to try to use some IDEs instead of code editors like VSCode. Many IDEs would automatically setup things like that for you if you just use the default settings. – Ranoiaetep Sep 09 '20 at 22:53

2 Answers2

1

First answer on stackoverflow so please forgive any mistakes(in presenting the answer)

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C++ Compile",
            "command": "C:\\Program Files\\mingw-w64\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${fileDirname}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],

        },
        {
            "type": "shell",
            "label": "C++ run",
            "command": ".\\${fileBasenameNoExtension}.exe",
          "dependsOn":["C++ Compile"],
         "dependsOrder": "sequence",
            "group": {
                          "kind": "build",
                           "isDefault": true
                     }
          
         }
    ]
}

the groups should be shifted from previous task(c++ compile) to new task (c++ run) and if .exe(code assumes it to be in workspace dir) is not found change the directory according to your config.

Tanishq
  • 11
  • 1
0

Well, since you can compile, and compiling is running an exe file(g++.exe), just copy the task again and change the exe file name, BAM! all done.

Do note you might need to change the "args" section.

And here's the finished tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C++ Compile",
            "command": "C:\\Program Files\\mingw-w64\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${fileDirname}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },{
            "type": "shell",
            "label": "WHATEVER LABEL YOU WANT HERE",
            "command": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
00001H
  • 22
  • 1
  • 7