How to compile and run a simple C program with Sub

2019-01-10 23:33发布

问题:

I want to compile a simple C program with GCC. What do I need to put in the sublime-build file to do so?

回答1:

Mac OS X:

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}"],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}

Windows:

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe"],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}


回答2:

The accepted answer did not work for me.

What I did is the following:

{
  "cmd" : ["make $file_base_name && ./$file_base_name"],
  "selector" : "source.c",
  "shell": true,
  "working_dir" : "$file_path",
}

Setting shell to true means it reads the cmd as one line, so I call make to compile and then run the script. The other option is to have shell set to false but you're unable to run multiple cmd. So the only way I got it to work was have it make the file with CMD + B and then run it with CMD + Shift + B:

{
  "cmd" : ["make", "$file_base_name"],
  "selector" : "source.c",
  "shell": false,
  "working_dir" : "$file_path",

  "variants": [
    {
      "cmd" : ["./$file_base_name"],
      "name": "Run"
    }
  ]
}


回答3:

In windows, compile and run, with file_regex

{
    "cmd": ["gcc", "$file_name", "-o", "${file_base_name}.exe", "&&", "${file_base_name}.exe"],
    "file_regex": "^([^:]+):([0-9]+):",
    "selector": "source.c",
    "shell": true,
    "working_dir": "$file_path"
}


回答4:

For Mac

{
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && osascript -e 'tell application \"Terminal\" to activate do script \"clear&&${file_path}/${file_base_name} && read -p \\\"Press Enter to exit.\\\"&&exit\"'"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",

"variants":
[
    {
        "name": "Run",
        "cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
    }
]

}

For windows

{
"cmd": ["g++", "${file}", "-o","${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:?(.*)$",
"working_dir": "${file_path}",
"encoding":"cp936",
"selector": "source.c",
"variants":
[
    {
    "name": "Run",
    "cmd": ["cmd","/C","start","cmd","/c", "${file_path}/${file_base_name}.exe &pause"]
    }
]
}

The configuration files above make you input data in the terminal(Mac) or cmd(windows),the output also was shown in the terminal or cmd。



回答5:

LINUX! COMPILING AND RUNNING IN TERMINAL, LANGUAGE C
Create a new Build System and paste this code:

{
"cmd": ["xterm-256color -e 'zsh -c \"gcc $file_name -o ${file_base_name} && ./${file_base_name} ;echo;echo Presiona ENTER para salir...; read line\"'"],
"selector" : "source.c",
"shell": true
}

echo $SHELL = To know which shell you use (zsh)-------------------------------------------------------------echo $TERM = To know which terminal you use (xterm-256color)-------------------------------------------

:D