【编译】gcc_strict-flags——适合 OIer 体质的编译脚本
  • 板块学术版
  • 楼主Estrella_Explore
  • 当前回复448
  • 已保存回复451
  • 发布时间2024/9/10 18:54
  • 上次更新2024/9/25 15:53:39
查看原帖
【编译】gcc_strict-flags——适合 OIer 体质的编译脚本
937928
Estrella_Explore楼主2024/9/10 18:54

简介

gcc_strict-flags 是一个采用 GPL-v3.0 开源协议的编译脚本。

将 OI 中常用的编译选项封装成一个 bash 脚本,开箱即用。

优点

g++ 启用适用于 OI 的最严格的编译选项,许多低级错误在编译期间就可以查出,使你专心于代码逻辑而非 debug 后大骂自己是憨憨

使用方法

对于待编译文件 foo.cpp,使用如下 bash 脚本编译:

sh make.sh foo.cpp

编译出的可执行文件名为 foo.out,且与 foo.cpp 在同一目录下。

自助排错指北

  • 终端运行时报错 Command not foundg++ 所在的文件夹需要被包括于环境变量 PATH,请将其添加到 PATH 中;

  • g++ 错误:unrecognized command-line option ‘--<某个参数>’g++ 版本过低,请升级 g++(建议),或者编辑 make.sh 以删掉这个参数(不建议);

注:2024-09-10 时开发者所使用的环境参数:Arch Linux 6.10.9-arch1-1 x86_64 GNU/Linux 以及 g++ 14.2.1 20240805。\

开发者仅保证较新版本的 g++ 正常使用,对于过于老旧的 g++,如 Dev-Cpp 5.11 自带的 g++ 4.8.1,请自行按照 ## 自助排错指北 (2) 解决

示例

例 1:

警告:if 的判断语句中出现赋值语句

#include <iostream>

int main() {
    int n = 114;
    if (n = 514) {
        std::cout << 114514 << std::endl;
    }
    return 0;
}

❯ sh ./make.sh test.cpp
test.cpp: In function ‘int main()’:
test.cpp:5:11: 警告:建议在用作真值的赋值语句前后加上括号 [-Wparentheses]
    5 |     if (n = 514) {
      |         ~~^~~~~

例 2:

#include <iostream>

int n = 113;

void foo(int a) {
    int n = 513;
    std::cout << a + n << std::endl;
}

int main() {
    foo(1);
    return 0;
}

❯ sh ./make.sh test.cpp
test.cpp: In function ‘void foo(int)’:
test.cpp:7:9: 警告:‘n’的声明隐藏了一个全局声明 [-Wshadow]
    7 |     int n = 513;
      |         ^
test.cpp:4:5: 附注:被隐藏的声明在这里
    4 | int n = 113;
      |     ^

例 3:

#include <iostream>

short mod = 998244353;
char ch = 114514;

int main() {
    std::cout << mod << std::endl << ch << std::endl;
    return 0;
}

❯ sh ./make.sh test.cpp
test.cpp:3:13: 警告:overflow in conversion from ‘int’ to ‘short int’ changes value from ‘998244353’ to ‘1’ [-Woverflow]
    3 | short mod = 998244353;
      |             ^~~~~~~~~
test.cpp:4:11: 警告:overflow in conversion from ‘int’ to ‘char’ changes value from ‘114514’ to ‘82’ [-Woverflow]
    4 | char ch = 114514;
      |           ^~~~~~

例 114514:

请自行探索吧


链接

GitHub 仓库地址

2024/9/10 18:54
加载中...