Sublime Forum

Compile and run in Mac Terminal Sublime Text files

#1

I’m working with a Mac and I want to compile and run my C files from the Terminal using a script. I’ve found a solution that apparently works quite well in Linux. Here is the article in Spanish (ayudasprogramacionweb.blogspot.c … 031640&m=1). It consists in two simple steps:

  1. You create a script in linux to compile and execute the source code in the Terminal and you store it in your Documents folder named as runC.sh.

#!/bin/bash gnome-terminal -e "/bin/bash -c 'gcc $1 -o $2; $2; echo; read -p 'Pulse_intro_pasa_salir...'; exit; exec /bin/bash'; &"

  1. Through a Sublime Text Build System, you call this script by passing as parameters the name of the source file.

{ "cmd": "~/Documents/runC.sh ${file_path}/${file_name} ${file_path}/${file_base_name}"], "shell": true }

I’ve tried it in my Mac but as I expected it doesn’t work… Sublime Text give me an error: “gnome-terminal: command not found”.
I have the gcc compiler installed and working and I have given permissions to the script using chmod +x ./Documents/runC.sh
Could you help me to adapt it for Mac OS please? I’ve just started programming and I don’t know where to start to fix it…

Thank you very much!

0 Likes

Build System Output
#2

Try either

{
    "cmd": "~/Documents/runC.sh", "${file_path}/${file_name}", "${file_path}/${file_base_name}"]
}

or

{
    "shell_cmd": "~/Documents/runC.sh \"${file_path}/${file_name}\" \"${file_path}/${file_base_name}\""]
}
0 Likes

#3

Thank you very much for your reply!
I’ve tried both options but they don’t work… With the first one Sublime Text return this error:

[Errno 2] No such file or directory: '~/Documents/runC.sh' [cmd: ['~/Documents/runC.sh', '/Users/Elle/Library/Application Support/Sublime Text 3/Packages/User/Hello.c', '/Users/Elle/Library/Application Support/Sublime Text 3/Packages/User/Hello']] [dir: /Users/Elle/Library/Application Support/Sublime Text 3/Packages/User] [path: /usr/bin:/bin:/usr/sbin:/sbin] [Finished]
With the second one Sublime Text doesn’t return any error, but nothing else either…
I have not experience but I think the problem is in the script not in the Build System…
Maybe I should change gnome-terminal for something else? I’ve also read the “command not found” error could be a problem with the PATH…

0 Likes

#4

Well, the embedded Python can not find the file ‘~/Documents/runC.sh’. Either it does not exists or it has problems resolving it somehow, maybe with the ~. Try another path or moving the file to a different place.

0 Likes

#5

FichteFoll thank you very much for your help but after days of searching I’ve finally found a solution! In Mac there is no need of a script, all that you need is the proper Build System.

This one for C files:

"cmd": "bash", "-c", "gcc '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]

And this one for C++ files:

"cmd": "bash", "-cpp", "g++ '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]

Now, when I press Cmd+B, a new Terminal window opens and my file runs, and of course it accepts input from the user, which sublime text console doesn’t…

Simple and easy! All the credits to this guy: stackoverflow.com/a/18562836/4359229

0 Likes