简体   繁体   English

如何在Sublime Text 2 [MAC OS X]上运行和编译.c

[英]How to Run and Compile .c on Sublime Text 2 [MAC OS X]

I am learning C at college now, and teachers told me to use codeblocks as an IDE, but in my opinion codeblocks is a bit ugly and that's why I've chosen Sublime Text 2, the BEST IDE/Text Editor out there. 我现在在大学学习C,老师告诉我使用代码块作为IDE,但在我看来,代码块有点难看,这就是为什么我选择Sublime Text 2,那里最好的IDE /文本编辑器。

At the moment I write my code via sublime, save it and then compile it via mac os terminal (gcc) and than run it on the terminal as well... 目前我通过sublime编写我的代码,保存它然后通过mac os终端(gcc)编译它,然后在终端上运行它...

What I want to know, if it is even possible, is how to do it right from sublime, using its console or a plugin (or something), in other words I want to know if it is possible to compile my .c and run it with only e few clicks right on sublime... (for now I am just building console applications) 我想知道的是,如果它是可能的话,是如何从sublime,使用它的控制台或插件(或其他东西)做到这一点,换句话说我想知道是否可以编译我的.c并运行只需点击几下即可获得...(现在我只是构建控制台应用程序)

I've read some posts here about this topic but none of those helped me to solve this. 我在这里阅读了一些关于这个主题的帖子,但没有一个帮助我解决这个问题。

A basic C build file could look like this: 基本的C构建文件可能如下所示:

{   
"cmd" : ["/path/to/gcc", "$file_name", "-o", "${file_base_name}", "-lgsl", "-lgslcblas", "-lm" , "-Wall"],
"selector" : "source.c",
"shell":false,
"working_dir" : "$file_path",

"variants":
    [
        {
            "name": "Run",
            "cmd": ["bash", "-c", "/path/to/gcc '${file}' -Wall -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
        }
    ]
}

To just compile you press command + b . 要编译你,请按命令 + b

To compile then run you would press command + shift + b 要编译然后运行,您将按下命令 + shift + b

The only thing you need to do is put the path to your gcc and inlcude the libraries you use (I left some GSL stuff for this example). 你唯一需要做的就是把路径放到你的gcc中并包含你使用的库(我为这个例子留下了一些GSL的东西)。 The $_variables are sublime build system variables and should not be changed. $_variables是sublime构建系统变量,不应更改。 For more info on those variables, look here . 有关这些变量的更多信息,请查看此处

You can put the actual build system file here: 您可以在此处输入实际的构建系统文件:

~/Library/Application Support/Sublime Text 2/Packages/User/C.sublime-build

I used the following as a .sublime-build to compile and run C. Basically an edit of the code used for C++. 我使用以下作为.sublime-build来编译和运行C.基本上编辑用于C ++的代码。 Worked for me. 为我工作。

{
"cmd": ["gcc", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c",

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

If you are using a makefile you could use something like this: 如果您使用的是makefile ,可以使用以下内容:

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

"variants":
    [
        {
            "name": "Run",
            "cmd": ["bash", "-c", "/usr/bin/make && '${file_path}/${file_base_name}'"]
        }
    ]
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM