Sublime text 3 compiles and executes C/C++ programs directly

1 work environment
(1) PC system: Ubuntu12.04LTS.
(2) editor version: Sublime Text 3
2 achieves its purpose
the background is that I recently started using Sublime Text 3 to edit code, found it very easy to use, and was attracted by its powerful plug-in features. However, using the build that comes with Sublime after editing C/C++ code isn’t easy, so I decided to customize a single-file C/C++ compilation command myself.
3 custom C compilation
(1) in the sublime toolbar, select “tools” -> “Compiling system” -& GT; “New build system” opens with a file name called “Untitled. Sublime -build”. Edit it, add the following code, save it as “myC.sublime-build” and make the path the default.

{
    //"shell_cmd": "make"
    "working_dir": "$file_path",
    "cmd": "gcc -Wall \"$file_name\" -o \"$file_base_name\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:?(.*)$",
    "selector": "source.c",

    "variants": 
    [
        {   
        "name": "Run",
            "shell_cmd": "gcc -Wall \"$file\" -o \"$file_base_name\" && \"${file_path}/${file_base_name}\""
        }
    ]
}

After saving, you’ll find something sublime in the “tools” -& GT; “Compiling system” -& GT; See myC’s build system under “New build system”.
(2) edit simple C code for testing

// File name:test_c_build.c
#include <stdio.h>

int main(int argc, char const *argv[])
{
    printf("hello world!\n");
    return 0;
}

Pressing the “Ctrl” + “Shift” + “b” brings up the compilation command selection window
select “myc-run” to compile and the results appear on the console below sublime, as follows:

4 custom C++ to compile
C++ the process is the same as that of C, only with a slightly different file content when creating a new compilation system:

{
    // "shell_cmd": "make"
    "encoding": "utf-8",
    "working_dir": "$file_path",
    "shell_cmd": "g++ -Wall -std=c++0x \"$file_name\" -o \"$file_base_name\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:?(.*)$",
    "selector": "source.cpp",

    "variants": 
    [
        {   
        "name": "Run",
            "shell_cmd": "g++ -Wall -std=c++0x  \"$file\" -o \"$file_base_name\" && \"${file_path}/${file_base_name}\""
        }
    ]
}

5 The difference between myC and MyC-Run is that myC only compiles, not executes; Myc-run, on the other hand, is directly executed after compilation.
6 solves the problem that sublime has no input in its own console
because sublime has no input in its own console, so if the program USES functions like cin, the program cannot be executed. In Windows system, you need to call CMD. exe and hand the console over to CMD, so you need to add the command to call CMD when compiling the configuration file.
modify the above myC++.sublime-build as follows,

{
    // "shell_cmd": "make"
    "encoding": "utf-8",
    "working_dir": "$file_path",
    "shell_cmd": "g++ -Wall -std=c++0x \"$file_name\" -o \"$file_base_name\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:?(.*)$",
    "selector": "source.cpp",

    "variants": 
    [
        {   
        "name": "Run",
            "shell_cmd": "g++ -Wall -std=c++0x  \"$file\" -o \"$file_base_name\" && \"${file_path}/${file_base_name}\""
        },
        {   
        "name": "RunInCmd",
            "shell_cmd": "g++ -Wall -std=c++0x  \"$file\" -o \"$file_base_name\" && start cmd /c \"\"${file_path}/${file_base_name}\" & pause \""
        }
    ]
}

It’s just adding this sentence,

&& start cmd /c \"\"${file_path}/${file_base_name}\" & pause \"

After saving,
write the sample program myC++_test_example.cpp. It is as follows:

#include <iostream>
#include <string.h>

using namespace std;
int main ()
{
    string str;

    cout << "please enter a string" << endl;
    cin >> str;
    cout << "input string: " << str << endl;

    return 0;
}


execution result:

7 currently only supports compiling single files, the rest will be discussed later.


Read More: