简体   繁体   English

在Mac Terminal Sublime Text文件中编译并运行

[英]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. 我正在使用Mac,并且想使用脚本从终端编译并运行C文件。 I've found a solution that apparently works in Linux. 我发现了一个显然可以在Linux中运行的解决方案。 Here is the article in Spanish ( http://ayudasprogramacionweb.blogspot.com.es/2013/01/ejecutar-en-terminal-linux-sublime-text.html ). 这是西班牙语文章( http://ayudasprogramacionweb.blogspot.com.es/2013/01/ejecutar-zh-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. 您在linux中创建一个脚本以在终端中编译和执行源代码,并将其存储在名为runC.sh的Documents文件夹中。

     #!/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. 通过Sublime Text Build System,您可以通过传递源文件的名称作为参数来调用此脚本。

     { "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? 我已经在Mac上尝试过了,但是正如我期望的那样,它无法正常工作……您能帮我将其适配于Mac OS吗? I've just started programming and I don't know where to start to fix it... thank you very much! 我才刚刚开始编程,但是不知道从哪里开始修复它……非常感谢!

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. 在Mac中,不需要脚本,您只需要适当的构建系统即可。

This one for C files: 这个用于C文件:

"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: 这是C ++文件:

"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... 现在,当我按Cmd + B时​​,将打开一个新的“终端”窗口并运行我的文件,并且它当然接受来自用户的输入,而Sublime文本控制台则不会...

Simple and easy! 简单容易! All the credits to this guy: https://stackoverflow.com/a/18562836/4359229 这个家伙的所有功劳: https : //stackoverflow.com/a/18562836/4359229

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sublime Text 3 使用传递的参数在 OS X 上编译和运行(在终端中)C - Sublime Text 3 compile and run (in terminal) C on OS X with passed arguments 如何在Sublime Text 2 [MAC OS X]上运行和编译.c - How to Run and Compile .c on Sublime Text 2 [MAC OS X] 如何在 sublime text 3 中编译和运行 C? - How to compile and run C in sublime text 3? 在Ubuntu的外部终端中使用Sublime Text 2运行一个简单的C程序 - Run a simple C program with Sublime Text 2 in external terminal on Ubuntu 如何在 Unix 控制台/Mac 终端中编译和运行 C/C++? - How to compile and run C/C++ in a Unix console/Mac terminal? 如何使用 Sublime Text 2 编译和运行一个简单的 C 程序? - How to compile and run a simple C program with Sublime Text 2? 如何在windows 10中的sublime text 3中编译和运行C程序? - How to compile and run C program in sublime text 3 in windows 10? 如何在Sublime Text 3 Mac OSX中构建和运行C程序 - How to build and run C programs in sublime text 3 mac osX 使用Sublime Text 2时,如何在终端中运行内置的可执行文件? - When using sublime text 2, how can I have the built executable run in terminal? 从Eclipse在Mac Terminal中使用ncurses.h运行和编译C程序 - Run and compile C program using ncurses.h in Mac Terminal from Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM