postgresql gcc编译选项解释

postgresql gcc编译选项解释

在使用make编译时候,输出的gcc选项:

gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -I../../../src/include  -D_GNU_SOURCE   -c -o postgres.o postgres.c

这条命令是用于编译 postgres.c 文件并生成目标文件 postgres.o 的 GCC(GNU Compiler Collection)编译命令。

  • -std=gnu99:指定 C 语言的标准为 GNU 99。
  • -Wall:打开几乎所有的警告信息。这有助于发现代码中的潜在问题。
  • -Wmissing-prototypes:警告那些没有声明原型的函数。这有助于确保函数的接口被明确声明,从而提高代码的可读性和可维护性。
  • -Wpointer-arith:对函数指针或者void *类型的指针进行算术操作时给出警告。

  • -Wdeclaration-after-statement:在C99之前,C语言不允许在代码块的中间声明变量(只能在块的开始处)。这个选项在C99模式下仍然会警告那些将变量声明放在代码块中间的情况,尽管这在C99中是合法的。

  • -Werror=vla:将变长数组(Variable Length Array, VLA)的警告提升为错误。这有助于避免使用可能导致栈溢出的变长数组。

  • -Wendif-labels:警告#else或#endif后面跟标识符。默认开启。

  • -Wmissing-format-attribute:警告那些应该使用format属性但未使用的函数(如printfscanf等函数的参数)。这有助于确保字符串格式化函数的参数类型正确。

  • -Wformat-security:检查格式字符串中的潜在安全问题,如缓冲区溢出。

  • -fno-strict-aliasing:禁用严格的别名规则。这允许编译器进行某些优化,但可能会引入一些难以发现的错误,特别是在涉及指针类型转换时。

  • -fwrapv:在整数运算溢出时,将结果包装(wrap around)到其表示范围内,而不是产生未定义行为。

  • -fexcess-precision=standard:控制浮点数的精度。这个选项确保在浮点运算中遵循C标准的规定,而不是使用可能更高精度的硬件特性。

  • -O2:启用优化选项 2。这会提高代码的运行速度,但可能会稍微增加编译时间。

  • -I:添加包含文件(头文件)的搜索路径。这意味着编译器会在这个路径下查找 #include 指令指定的文件。

  • -D_GNU_SOURCE:定义 _GNU_SOURCE 宏。这会让编译器和链接器包括 GNU 特有的特性,如一些 GNU C 库中的扩展函数。

-Wpointer-arith举例

#include <stdio.h>  
  
// 函数声明  
int add(int a, int b);  
int subtract(int a, int b);  
  
// 函数指针类型定义  
typedef int (*Operation)(int, int);  
  
// 使用函数指针的示例函数  
int calculate(Operation op, int a, int b) {  
    return op(a, b);  
}  
  
// 函数实现  
int add(int a, int b) {  
    return a + b;  
}  
  
int subtract(int a, int b) {
    return a - b;  
}  
  
int main() {  
    Operation op1 = add;
    Operation op2 = subtract;
    Operation op3 = add + 1;//对函数指针进行运算
  
    printf("%d + %d = %d\n", 5, 3, calculate(op1, 5, 3));  
    printf("%d - %d = %d\n", 5, 3, calculate(op2, 5, 3));  
  
    return 0;  
}

gcc -Wpointer-arith -c hello.c

hello.c: In function ‘main’:
hello.c:27:25: warning: pointer to a function used in arithmetic [-Wpointer-arith]
     Operation op3 = add + 1;//对函数指针进行运算

-Wdeclaration-after-statement

#include <stdio.h>

int main(int argc, char *argv[]) {
    int var = 10;
    printf("var = %d\n", var);
    int var1 = 10; // 声明在语句之后,将会报错
    printf("var1 = %d\n", var1);
    return 0;
}

gcc -Wdeclaration-after-statement hello.c

hello.c: In function ‘main’:
hello.c:6:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
     int var1 = 10; // 声明在语句之后,将会报错
     ^

-Wendif-labels

#include <stdio.h>

int main()
{
#if FOO
//...
//...
#endif FOO

    return 0;
}

默认就是开启的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shulu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值