How to configure VS Code for building and compiling C++ by Control-Shift-B?

In C++ in VSCode I wrote a program and when I wanted to configure it I created and configured a tasks.json and c_cpp_properties.json and then command-shift-b d it and that built it (created the binary file) and then in the integrated terminal I had to ./(thebinaryfile) for it to run. What code do I have to put in tasks.json OR c_cpp_properties in order for it to not only build, but also run, when I hit Control-shift+b? From which JSON file do I alter the command for execution and what code should I add/replace? I have read all the MS provided docs and Googled but still find myself confused.

My c_cpp_properties.json file is https://pastebin.com/90RLnf5S.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "/usr/local/include", "/Users/me/Downloads/SFML-2.5.1-macos-clang/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

My tasks.json file is https://pastebin.com/m9x3nFqq

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        },
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-I/Users/me/Downloads/SFML-2.5.1-macos-clang/include",
                "-L/Users/me/Downloads/SFML-2.5.1-macos-clang/lib",
                "-lsfml-graphics",
                "-lsfml-window",
                "-lsfml-system",
                "-lsfml-audio"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },
        {
            "type": "shell",
            "label": "clang build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ]
}

My code is https://pastebin.com/smZVfitp .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <SFML/Graphics.hpp>
 
int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);
 
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
 
        window.clear();
        window.draw(shape);
        window.display();
    }
 
    return 0;
}
Last edited on
Please reply
Hello HelenaMagic,

In both the versions of VS I have used, 2015 and 2017, the "Ctrl + Shift + B" shortcut only build, or compile, the file.

If you want to run the program use either the button that is labled "Local Windows Debugger" or the "F5" function key.

I actually managed to put a button next to "Local Windows Debugger" to compile the code.

Andy

Edit: typo
Last edited on
@Handy Andy,

she is using Visual Studio Code(which is an editor, not an IDE) not Visual Studio.
@Learner2,

Thank you for the correction. I did not know that.

Andy
Topic archived. No new replies allowed.