Elle518
Elle518

Reputation: 59

Compile and run in Mac Terminal Sublime Text files

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 in Linux. Here is the article in Spanish (http://ayudasprogramacionweb.blogspot.com.es/2013/01/ejecutar-en-terminal-linux-sublime-text.html). 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 -Wall $1 -o $2; ./$2; echo; read -p 'Press_enter_to_scape...'; exit; exec /bin/bash'; &"
    
  2. 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_name} ${file_base_name}"],
        "shell": true
    }
    

I've tried it in my Mac but as I expected it doesn't work... 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!

Upvotes: 0

Views: 2463

Answers (1)

Elle518
Elle518

Reputation: 59

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: https://stackoverflow.com/a/18562836/4359229

Upvotes: 1

Related Questions