跳到正文

个人记录

一个 OIer 的编译命令 & 配置文件

W_C_B_H约 19 分钟
原文

这是 2026年8月28日 存档的第 6 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月7日
收录
2026年2月7日
更新
2026年10月6日
篇幅
约 19 分钟

版本

  1. v8当前
  2. v7
  3. v6
  4. v5
更早的 4 个版本
  1. v4
  2. v3
  3. v2
  4. v1

编译命令

(NOI)Linux

  1. 在 ~/.bashrc 末尾添加:
Bash
cpl() {
    g++ "./$1.cpp" -o "./$1" -O2 -std=gnu++14 -static -Wall -Wextra -Wshadow -Wno-unused-result -Wno-sign-compare -Wl,-z,stack-size=1000000000 -fsanitize=undefined,bounds -DLOCAL && echo -e "\e[32mCompiled ./$1.cpp\e[0m"
}
run() {
    cpl "$1" && echo -e "\e[36mRunning ./$1\n---------------begin---------------\e[0m" && local TIMEFORMAT=$'\e[33m\n----------------end----------------\nTime: %3Rs\e[0m' && time "./$1"
}
  1. 可在控制台内运行如下命令以重新加载 ~/.bashrc 中的命令:
Bash
source ~/.bashrc

Windows

  1. 在(VSCode 中的)Powershell 内运行(用户名不是 Administrator 的自行改为实际用户名):
Bash
cd C:\Users\Administrator\Documents
mkdir .\WindowsPowerShell
cd .\WindowsPowerShell
type nul > Microsoft.VSCode_profile.ps1
type nul > Microsoft.PowerShell_profile.ps1
code Microsoft.VSCode_profile.ps1
code Microsoft.PowerShell_profile.ps1
  1. 在打开的文件中添加:
Bash
function cpl {
    param([string]$name)
    $cppFile = ".\$name.cpp"
    $exeFile = ".\$name.exe"
    if (-not (Test-Path $cppFile)) { 
        Write-Error "Error: $cppFile not found"; return 
    }
    
    Write-Host "Compiling $cppFile" -ForegroundColor Green
    $swCompile = [System.Diagnostics.Stopwatch]::StartNew()
    
    g++ "$cppFile" -o "$exeFile" -O2 -std=gnu++14 -static -Wall -Wextra -Wshadow -Wno-unused-result -Wno-sign-compare "-Wl,--stack=1000000000" -DLOCAL # 神秘 Windows 没法加 "-fsanitize=undefined,bounds"
    
    $swCompile.Stop()
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host "Compile Time: $($swCompile.Elapsed.TotalSeconds.ToString('0.000')) s" -ForegroundColor Yellow
    }
}

function run {
    param([string]$name)
    cpl $name
    if ($LASTEXITCODE -eq 0 -and (Test-Path ".\$name.exe")) {
        Write-Host "Running .\$name.exe" -ForegroundColor Green
        Write-Host "---------------begin---------------" -ForegroundColor Cyan
        $swRun = [System.Diagnostics.Stopwatch]::StartNew()
        & ".\$name.exe"
        $swRun.Stop()
        Write-Host "`n----------------end----------------" -ForegroundColor Yellow
        Write-Host "Run Time: $($swRun.Elapsed.TotalSeconds.ToString('0.000')) s" -ForegroundColor Yellow
    }
}
  1. 可在(VSCode 中的)Powershell 内运行如下命令以重新加载 PowerShell 配置文件:
Bash
. $PROFILE
  1. 在部分特殊情况下,可以使用丐版命令来编译运行(将 $name 的值替换为程序实际路径并去掉 .cpp 即可):
Bash
$name = ".\"; Write-Host "Compiling $name.cpp" -ForegroundColor Green; $swCompile = [System.Diagnostics.Stopwatch]::StartNew(); g++ "$name.cpp" -o "$name.exe" -O2 -std=gnu++14 -static -Wall -Wextra -Wshadow -Wno-unused-result "-Wl,--stack=1000000000" -DLOCAL; $swCompile.Stop(); Write-Host "Compile Time: $($swCompile.Elapsed.TotalSeconds.ToString('0.000')) s" -ForegroundColor Yellow; Write-Host "Running $name.exe" -ForegroundColor Green; Write-Host "---------------begin---------------" -ForegroundColor Cyan; $swRun = [System.Diagnostics.Stopwatch]::StartNew(); & "$name.exe"; $swRun.Stop(); Write-Host "`n----------------end----------------" -ForegroundColor Yellow; Write-Host "Run Time: $($swRun.Elapsed.TotalSeconds.ToString('0.000')) s" -ForegroundColor Yellow

配置文件

VSCode cpp.json

JSON
    "C++ Template": {
        "prefix": "#cpp",
        "body": [
            "#include <bits/stdc++.h>",
            "using namespace std;",
            "#define int long long",
            "#define eb emplace_back",
            "#define fi first",
            "#define se second",
            "#define all(a) (a).begin(), (a).end()",
            "using ull = unsigned long long;",
            "using i128 = __int128;",
            "using ld = long double;",
            "using pii = pair<int, int>;",
            "using p3i = pair<int, pii>;",
            "const int inf = 0x3f3f3f3f, Inf = 0x3f3f3f3f3f3f3f3f;",
            "template<typename T, typename U> void chkmin(T &x, U y) { x > y && (x = y); }",
            "template<typename T, typename U> void chkmax(T &x, U y) { x < y && (x = y); }",
            "",
            "char buf[1 << 20], *p1 = buf, *p2 = buf;",
            "#ifdef LOCAL",
            "    #if defined(__linux__) || defined(__linux)",
            "        #define gc getchar_unlocked",
            "    #elif defined(_WIN32) || defined(_WIN64)",
            "        #define gc _getchar_nolock",
            "    #else",
            "        #define gc getchar",
            "    #endif",
            "#else",
            "    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)",
            "#endif",
             "",
            "namespace IO { bool read(int &x) { x = 0; char c = gc(); bool f = 0; while (!isdigit(c)) { f |= c == '-';",
            "if (c == EOF) { return 0; } c = gc(); } while (isdigit(c)){ x = (x << 3) + (x << 1) + (c ^ 48); c=gc(); }",
            "if (f) { x=-x; } return 1; } bool read(string &s){ s = \"\"; char c=gc(); while (!isprint(c) || isspace(c))",
            "{ if (c == EOF) { return 0; } c = gc(); } while(isprint(c) && !isspace(c)){ s+=c; c = gc(); } return 1; }",
            "bool read(char &c) { c = gc(); while (!isprint(c) || isspace(c)){ if (c == EOF) { return 0; } c = gc(); }",
            "return 1; } template < typename T, typename U > bool read(pair<T, U> &x) { if (!read(x.fi)) { return 0; }",
            "return read(x.se); } template < typename T, typename ... U > bool read(T & x, U &... y) { if (!read(x)) {",
            "return 0; } return read(y...); } void write(int x) { if(x < 0){ putchar('-'); x = -x; } string s = \"\"; do",
            "{ s += x % 10 ^ 48; x /= 10; } while(x); for (int i = s.size() - 1; ~i; i--) putchar(s[i]); } void write(",
            "char c) { putchar(c); } void write(const char *s) { while(*s) putchar(*s++); } void write(string x) { for",
            "(char c : x) putchar(c); } template <typename T, typename... U> void write(T x, U... y) { write(x); write",
            "(y...); } template <typename T, typename U> void write(pair <T, U> &x) { write(x.fi); putchar(' '); write",
            "(x.se); } template <typename T, typename... U> void writeln(T x, U ... y) { write(x, y...); puts(\"\"); } }",
            "",
            "namespace W_C_B_H {",
            "    using namespace IO;",
            "    void main() {",
            "    }",
            "}",
            "signed main() {",
            "    // freopen(\".in\", \"r\", stdin);",
            "    // freopen(\".out\", \"w\", stdout);",
            "    W_C_B_H::main();",
            "    return 0;",
            "}"
        ],
        "description": "C++ 缺省源"
    }

VSCode settings.json

JSON
{
    "C_Cpp.errorSquiggles": "disabled",
    "files.autoSave": "onFocusChange",
    "editor.fontSize": 16,
    "editor.renderWhitespace": "boundary",
    "editor.wordWrap": "on"
    "workbench.colorTheme": "Quiet Light",
    "editor.trimAutoWhitespace": false
}

Dev-C++ 缺省源

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define eb emplace_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
using ull = unsigned long long;
using i128 = __int128;
using ld = long double;
using pii = pair<int, int>;
using p3i = pair<int, pii>;
const int inf = 0x3f3f3f3f, Inf = 0x3f3f3f3f3f3f3f3f;
template<typename T, typename U> void chkmin(T &x, U y) { x > y && (x = y); }
template<typename T, typename U> void chkmax(T &x, U y) { x < y && (x = y); }

char buf[1 << 20], *p1 = buf, *p2 = buf;
#ifdef LOCAL
	#if defined(__linux__) || defined(__linux)
		#define gc getchar_unlocked
	#elif defined(_WIN32) || defined(_WIN64)
		#define gc _getchar_nolock
	#else
		#define gc getchar
	#endif
#else
	#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
#endif

namespace IO { bool read(int &x) { x = 0; char c = gc(); bool f = 0; while (!isdigit(c)) { f |= c == '-';
if (c == EOF) { return 0; } c = gc(); } while (isdigit(c)){ x = (x << 3) + (x << 1) + (c ^ 48); c=gc(); }
if (f) { x=-x; } return 1; } bool read(string &s){ s = ""; char c=gc(); while (!isprint(c) || isspace(c))
{ if (c == EOF) { return 0; } c = gc(); } while(isprint(c) && !isspace(c)){ s+=c; c = gc(); } return 1; }
bool read(char &c) { c = gc(); while (!isprint(c) || isspace(c)){ if (c == EOF) { return 0; } c = gc(); }
return 1; } template < typename T, typename U > bool read(pair<T, U> &x) { if (!read(x.fi)) { return 0; }
return read(x.se); } template < typename T, typename ... U > bool read(T & x, U &... y) { if (!read(x)) {
return 0; } return read(y...); } void write(int x) { if(x < 0){ putchar('-'); x = -x; } string s = ""; do
{ s += x % 10 ^ 48; x /= 10; } while(x); for (int i = s.size() - 1; ~i; i--) putchar(s[i]); } void write(
char c) { putchar(c); } void write(const char *s) { while(*s) putchar(*s++); } void write(string x) { for
(char c : x) putchar(c); } template <typename T, typename... U> void write(T x, U... y) { write(x); write
(y...); } template <typename T, typename U> void write(pair <T, U> &x) { write(x.fi); putchar(' '); write
(x.se); } template <typename T, typename... U> void writeln(T x, U ... y) { write(x, y...); puts(""); } }

namespace W_C_B_H {
	using namespace IO;
	void main() {
	}
}
signed main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	W_C_B_H::main();
	return 0;
}