Auto run .exe file after building c++ using sublime text 3

299 views Asked by At

I found this build configuration online:

{
  "cmd": ["C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++", "-o", "${file_path}/${file_base_name}.exe", "-static-libgcc", "-static-libstdc++", "${file_path}/${file_base_name}.cpp"],
  "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
  "selector": "source.cpp",
  "shell": true,
  "variants":[
        {
            "name": "Run",
            "cmd": ["start","cmd", "/K" , "$file_base_name"]
        }
    ],
}

Is there any way to AUTOMATICALLY run the .exe program after it gets built rather than separately using CTRL+B to build and CTRL+SHIFT+B to run?

I tried this:

{
  "cmd": ["C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++", "-o", "${file_path}/${file_base_name}.exe", "-static-libgcc", "-static-libstdc++", "${file_path}/${file_base_name}.cpp"],
  "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
  "selector": "source.cpp",
  "shell": true,
  "cmd": ["start","cmd", "/K" , "$file_base_name"]
}

But it only displays the older .exe file rather than the newly built one.

1

There are 1 answers

0
OdatNurd On

The build systems for C and C++ that ship with Sublime out of the box do exactly that; their names are C Single File and C++ Single File (differing only in using g++ versus gcc):

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",

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

The main build compiles only, the variant compiles, then runs.

This requires that your compiler be properly set up and added to the PATH. Note also that it runs the build directly inside of Sublime; if you want it to execute in an external terminal window like that build does, you will need to modify the command that runs it accordingly.

If your program expects to gather input interactively from the user, you will need to either run the program externally or use the Terminus package to make the build interactive. The README has instructions as well as a link to a tutorial video that describes how to convert the build.