本文将着重介绍 Sublime 如何配置 C++ 环境,并给出方便 ACMer 的模板使用方式。

Sublime 作为文本编辑器的同时还可以进行一些轻量级的编程运算,由于其漂亮的界面,深得 ACMer 的喜爱。

虽然 Sublime 自带一个C语言的编译功能,但却无法使用,需要一定的配置才可以运行。

安装MinGW

要想编译C语言首先要有编译器,Windows平台主要是 gccg++,是通过安装 MinGW 实现的。

MinGW 的官网是 http://www.mingw.org/ ,但是从官网安装很麻烦,在线安装经常龟速容易失败。

博主推荐的方法是借助 codeblocks,选择带有MinGW的版本安装(100M以上)。

博主也提供一个 codeblocks-16.01mingw-setup 的百度云下载,感觉codeblocks官网还是下载慢。

安装后把MinGW文件夹复制出来放到C盘根目录就可以了。

环境变量配置

右键计算机->属性->高级系统设置->环境变量

C:\MinGW\bin 添加到path变量中,注意前后英文分号。

Build System 配置

Tools -> Build System -> New Build System

将下面代码粘贴,并保存为 gcc.sublime-build

Windows 系统代码如下

{
"encoding": "cp936",
"working_dir": "$file_path",
"shell_cmd": "g++ -Wall -std=c++0x -fexec-charset=GBK $file_name -o $file_base_name",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"selector": "source.c++",

"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -Wall -fexec-charset=utf-8 \"$file\" -o \"$file_base_name\" && start cmd /c \"\"${file_path}/${file_base_name}\" & pause\""
}
]
}

MAC 系统代码如下

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

"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -o \"${file_path}/${file_base_name}\" \"${file}\" && open \"${file_path}/${file_base_name}\""
}
]
}

这样就可以用cmd运行,并且scanf也能读取。

模板配置

sublime我认为最方便的地方就是可以设置一些模板,比如博主参加ACM竞赛开头会有很多在每道题都出现的语句,头文件等,如果有模板就会非常方便。

<snippet>
<content><![CDATA[
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <stack>
#include <queue>
#define LL long long
#define INF 0x3f3f3f3

using namespace std;

const double eps = 1e-8;
const int MAXN = (int)1e5 + 5;
const LL MOD = 1000000007;

int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
#endif

int cas;
scanf("%d", &cas);
while(cas--)
{
${1}
}
return 0;
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<!-- <tabTrigger>hello</tabTrigger> -->
<tabTrigger>acm</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
<scope>source.c++</scope>
</snippet>

中间代码的位置当然可以修改。然后保存为文件名.sublime-snippet。例如博主起名为 acm.sublime-snippet,之后只要用sublime打开 .cpp 文件,输入 acm -> 回车就可以显示中间代码。是不是很方便啊~

演示效果如下: