Sublime Text runs C and C + + programs

Posted by eddedwards on Wed, 12 Jan 2022 06:37:48 +0100

Ref: http://c.biancheng.net/view/8094.html

Sublime Text runs C and C + + programs

Sublime Text is a very popular text editor at present. It has powerful functions (with many plug-ins), simple interface and supports cross platform use (including Mac OS X, Linux and Windows).

In the eyes of programmers, Sublime Text is not only a text editor, but also a code editor. As long as Sublime Text is simply set, it can call the GCC compiler to compile the written code.

Considering the cross platform characteristics of sublime text, this section will explain to readers how to set up sublime text editors in Windows, Linux (taking Ubuntu as an example) and Mac OS X systems to enable them to execute C and C + + programs.

Configure Sublime Text for Windows

As shown in Figure 1, it is the Sublime Text that the author has installed on this machine. If the reader has not installed it, go first Sublime Text official website Download and install (the installation process is very simple and will not be repeated here).
You should know that Sublime Text just installed cannot run C and C + + code by itself. We need to set it manually.

0. However, before setting, the reader should ensure that the GCC compilation environment has been initialized in the current system. Open the command line window and execute the gcc -v instruction. If the specific version of gcc compiler is output, it indicates that the current system has successfully configured the GCC compilation environment (as shown in Figure 2).

If the GCC compilation environment has not been configured in the environment used by the reader, you can read< MinGW download and installation tutorial> (https://osdn.net/projects/mingw/ )Install in section. (or go to my blog to download the MinGW.exe file: https://i.cnblogs.com/files)

After the GCC compiler has been installed, start to formally configure the Sublime Text editor.

1) To enable Sublime Text to run C language programs, click "tools - > build system - > new build system" in the menu bar to open a temporary file in Sublime Text, as shown below:

Delete all its contents and completely copy the following contents into the file:
{
    "cmd": ["gcc","${file}","-o", "${file_path}/${file_base_name}"],
    "file_regex":"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir":"${file_path}",
    "selector": "source.c",
    "encoding":"cp936",
    "variants":
    [
        {
            "name": "C_Run",
            "cmd": ["cmd","/c", "gcc", "${file}", "-o", "${file_path}/${file_base_name}","&&", "cmd", "/c","${file_path}/${file_base_name}"]
        },
        {
            "name":"C_RunInCommand",
            "cmd": ["cmd","/c", "gcc", "${file}","-o","${file_path}/${file_base_name}", "&&","start", "cmd", "/c","${file_path}/${file_base_name} & pause"]
         }
    ]
}

  

The contents in this file are actually gcc instructions that need to be executed when running C language programs, but they need to be written in the above form in the Sublime Text file.

Press Ctrl+S to save the file, where the file name is untitled by default Sublime build (untitile can be customized, such as gcc_sublime build). The saved path is the path popped up by pressing Ctrl+S (do not modify this path manually).

2) Reopen Sublime Text and select "tools - > build system" in the menu bar. In this option, you can see the gcc created in the previous step_ The file name of sublime build is gcc, as shown in Figure 4:

By checking the gcc compilation option (click directly in Figure 4), we can directly run the written C language program in Sublime Text. For example, write the following program in Sublime Text:
On this basis, select "tools - > build with..." Option, Sublime Text will pop up the following dialog box:
As you can see, here are GCC and gcc-C_Run and GCC runincommand options, where GCC is used to compile programs (readers can view the execution results by themselves), and gcc-C_Run is used to call the gcc compiler inside Sublime Text and display the execution result of the program (as shown in Figure 7 a), gcc_RunInCommand (this editor is recommended) is used to run the program with GCC instructions in the command line window and output the execution results (as shown in Figure 7 b)).
Similarly, if you want Sublime Text to have the ability to execute C + + programs, you only need to create another G++_ Sublime build configuration file, and copy the following contents into the file:
{
    "cmd": ["g++","-Wall", "${file}", "-o", "${file_path}/${file_base_name}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "encoding":"cp936",
    "variants":
    [
        {
            "name": "C++_Run",
            "cmd": ["cmd", "/c", "g++", "-Wall","${file}", "-o", "${file_path}/${file_base_name}", "&&", "cmd", "/c", "${file_path}/${file_base_name}"]
        },   
        {
            "name": "C++_RunInCommand",
            "cmd": ["cmd", "/c", "g++", "-Wall","${file}", "-o", "${file_path}/${file_base_name}", "&&", "start", "cmd", "/c", "${file_path}/${file_base_name} & echo.&pause"]
        }
    ]
}

Other operation steps are exactly the same as above. Thus, sublime can be made_ Text has the ability to execute C + + programs.

Configuring Sublime Text for Mac OS X

The configuration process of enabling Sublime Text to execute C and C + + in Mac OS X system is roughly the same as that in Windows system, with the following two differences:
  • By default, the gcc compiler is installed in the operating system by default, so if the reader has no deliberate requirements for the compiler version, the process of installing GCC can be omitted;
  • In Mac OS X systems, GCC needs to be modified_ Content in the sublime build file.

If you want Sublime Text to have the ability to execute C language programs, click "tools - > build system - > new build system" in the menu bar to create a GCC Sublime build configuration file, the contents of which are as follows:
{
    "cmd" : ["gcc -o ${file_base_name} $file_name"],
    "shell" : true,
    "working_dir" : "$file_path",
    "selector": "source.c",
    "variants" :
    [{
        "name" : "c_Run",
        "cmd" : "./${file_base_name}"
    },
    {
      "name": "c_RunInCommand",
      "shell_cmd": "open -a Terminal.app '${file_base_name}'"
    }]
}

Where, c_Run is used to display the execution result of the program at the bottom of Sublime Text; And c_RunInCommend is used to display the execution results of C language programs in the command line window. Still taking the C language program in Figure 5 as an example, the execution effects of the two compilation options are as follows:

It can be seen that using the above instruction to call the command line window to execute the C language program, in addition to the execution results, other useless information will be displayed. Therefore, it is recommended that readers use the c-Run compilation option to execute C language programs, which is simple and convenient.

 

Similarly, by creating G + + Sublime build configuration file, and copy the following contents into the file:
{
    "cmd" : ["g++ -o ${file_base_name} $file_name"],
    "shell" : true,
    "working_dir" : "$file_path",
    "selector": "source.cpp",
    "variants" :
    [{
        "name" : "c_Run",
        "cmd" : "./${file_base_name}"
    },
    {
      "name": "c_RunInCommend",
      "shell_cmd": "open -a Terminal.app '${file_base_name}'"
    }]
}

You can make Sublime Text have the ability to execute C + + programs.

Configuring Sublime Text for Ubuntu

You should know that the gcc compiler is not installed by default in the Ubuntu system. Therefore, before configuring Sublime Text, readers should install the gcc compiler by themselves (you can install it by executing the sudo apt install gcc instruction).

After installing the GCC compiler, you can start configuring Sublime Text. The configuration process of Sublime Text is roughly the same as that in the Windows environment. The only difference is that you need to modify xxx The content in the sublime build configuration file (readers can customize the content of xxx).

If you want Sublime Text to have the ability to execute C language programs, you can select "tools - > build system - > new build system" and create a GCC Sublime build configuration file, which contains the following contents:
{
    "encoding": "utf-8",
    "working_dir": "$file_path",
    "shell_cmd": "gcc \"$file_name\" -o \"$file_base_name\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "selector": "source.c",

    "variants":
    [
    
     {   
        "name": "c_Run",
        "shell_cmd": "./${file_base_name}"
        },
        {   
        "name": "c_RunInCommand",
        "shell_cmd": "gnome-terminal -x bash -c \"'${file_path}/${file_base_name}';read -p '\nPress any key to continue...'\""
        }
    ]
}

If you want Sublime Text to have the ability to execute C + + programs, you can create a G + + Sublime build configuration file, copy the following contents to the file:

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

    "variants":
    [
    
     {   
        "name": "c++_Run",
        "shell_cmd": "./${file_base_name}"
        },
        {   
        "name": "c++_RunInCommand",
        "shell_cmd": "gnome-terminal -x bash -c \"'${file_path}/${file_base_name}';read -p '\nPress any key to continue...'\""
        }
    ]
}

This completes the configuration of Sublime Text. Still taking the C language program in Figure 5 as an example, the following is the effect diagram of using Sublime Text to execute it:

Topics: Python