Search code examples
linuxsublimetext3gnome-terminal

SublimeText build system for C/C++ in Terminal.


I am using "Linux Centos-7" and here I install "SublimeText-3". C/C++ codes are running well in SublimeText Console but I want to run those code in Linux terminal. I include a couple line in SublimeText build system like--

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

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

Above code is only create a executable file. which I have to run manually in terminal.

And,

{
"cmd": ["g++", "$file", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cxx, source.cpp",
"variants":
[
    {
        "cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
        "variants":
[
    {
        "name": "Run",
        "shell": true,
        "cmd": ["gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};echo;echo;  echo Press ENTER to continue; read line;exit; exec bash\"'"]
    }
]  
    }

]    }

This code can run only existing file in terminal.

Now, I just want to write code in SublimeText and execute it in terminal automatically.

Thanks.


Solution

  • I was in the same situation some days ago after I installed Sublime. I researched for a bit and ended up with this.

    {
      "cmd": ["g++", "-std=c++0x", "$file", "-o", "${file_path}/${file_base_name}"],
      "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
      "working_dir": "${file_path}",
      "selector": "source.c, source.c++, source.cxx, source.cpp",
      "variants": 
      [ 
        {
          "name": "Run",
          "cmd": ["bash", "-c", "g++ -std=c++0x '${file}' -o '${file_path}/${file_base_name}' &&  xterm -e bash -c '${file_path}/${file_base_name}; read'"]  
        }
      ]    
    }
    

    You probably have to install xterm too.