跳到正文

科技·工程

【进阶】在 Sublime Text 编写 Build System

ケロシ约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年3月5日
收录
2026年3月6日
更新
2026年10月18日
篇幅
约 5 分钟

版本

  1. v1当前

建议先阅读:上期回顾:【重制】如何在 NOI 系列比赛中使用 Sublime Text

这是我们去年的 Build System:

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

	"variants":
	[
		{
			"name": "Run",
			"shell_cmd": "ulimit -s 114514 && g++ \"${file}\" -O2 -std=c++14 -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
		}
		,
		{
			"name": "Debug",
			"shell_cmd": "g++ \"${file}\" -g -O2 -std=c++14 -o \"${file_path}/${file_base_name}\""
		}
	]
}

是时候升级了!

首先我们的目标:

  1. 可以选择在命令窗口里运行
  2. 快速 gdb 调试

为了不让所有东西都挤在 g++.sublime-build 文件里,我们这里选择写几个脚本。

窗口运行

关键就是调用 gnome-terminal,

记得开栈,然后最后把当前进程替换成一个 bash,防止窗口消失:

Bash
#!/bin/bash

g++ "$1" -o "$2" -std=c++14 -O2

if [ $? -eq 0 ]; then
	gnome-terminal -- bash -c " ulimit -s 114514; ./'$2'; exec bash "
else
	echo "CE"
fi

然后也可以加个 /bin/time 来看时间和动态空间:

Bash
#!/bin/bash

g++ $1 -o $2 -std=c++14 -O2

if [ $? -eq 0 ]; then
	gnome-terminal -- bash -c " ulimit -s 114514; /bin/time -f 'Time: %U s\nMemory: %M KB' ./$2; exec bash "
else
	echo "CE"
fi

快速 gdb 调试

编写 debug.sh,

编译后调用 gdb 并开启图形化,

记得不要开 O2,不然 RE 信息可能会丢失:

Bash
#!/bin/bash

g++ "$1" -o "$2" -std=c++14 -g

if [ $? -eq 0 ]; then
	gnome-terminal -- bash -c " gdb '$2' -ex 'tu e' "
else
	echo "CE"
fi

最后全部贴进 Build System 就行了。

脚本路径可以是绝对路径,每道题公用,

或者每道题有个对于的脚本文件也可以:

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

	"variants":
	[
		{
			"name": "Run",
			"shell_cmd": "ulimit -s 114514 && g++ \"${file}\" -O2 -std=c++14 -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
		}
		,
		{
			"name": "Shell",
			"shell_cmd": "bash \"${file_path}/run.sh\" \"${file}\" \"${file_base_name}\""
		}
		,
		{
			"name": "Time",
			"shell_cmd": "bash \"${file_path}/time.sh\" \"${file}\" \"${file_base_name}\""
		}
		,
		{
			"name": "Debug",
			"shell_cmd": "bash \"${file_path}/debug.sh\" \"${file}\" \"${file_base_name}\""
		}
	]
}
原文
存档评论2 条抓取于