编译选项的使用

1.禁止隐式声明

-Werror=implicit-function-declaration 编译选项中加了这个,隐式声明不过,报error而不是warning了

复制代码
eg: test.c
int main(int argc, char *argv[])
{
    char *pstr = "hello nihao";
    printf("l pstr=%s\n", pstr);
}

[ubuntu @tmp]$ gcc test.c -o pp
test.c: In function ‘main’:
test.c:5:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
  printf("l pstr=%s\n", pstr);


[ubuntu @tmp]$ gcc test.c -Werror=implicit-function-declaration  -o pp
test.c: In function ‘main’:
test.c:5:2: error: implicit declaration of function ‘printf’ [-Werror=implicit-function-declaration]
  printf("l pstr=%s\n", pstr);
  ^
test.c:5:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
cc1: some warnings being treated as errors
复制代码

 

2. __unused 加载参数后面表示参数不使用,就不会报参数没有使用的警告了

int main(int argc __unused, char *argv[] __unused) 
{
    return 0;      
}

 

3. 屏蔽报错

复制代码
cc_defaults {
    name: "test_flags",

    cflags: [
        "-Wno-unused-variable", //让这个警告不报成Error
        "-Wno-unused-parameter",
        "-Wno-address-of-packed-member"
    ],

    shared_libs: ["liblog"],
}
复制代码

将 "-Wunused-parameter" 形式修改为 "Wno-unused-parameter" 形式以取消警告导致中断编译,将"-W"修改为"Wno-",-Wunused-*之类所有问题可以套这个万能公式。

 

4. C++中若不想让"Werror"对没有使用后的函数报错,可以使用 [[maybe_unused]] static String16 get_interface_name() 进行修饰。

 

参考:

https://blog.csdn.net/u010164190/article/details/131525733

 

posted on   Hello-World3  阅读(424)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示