gcc开启/关闭 编译警告/报错 -Werror

本文详细介绍了GCC编译器中的多种实用选项,包括如何让编译在首次遇到错误时停止、如何将警告信息视为错误、如何指定静态链接及动态链接等。此外还介绍了如何开启特定的警告信息,如未使用的变量警告、浮点数相等比较警告等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候我们编译一个大的项目的时候,会出现很多错误使得屏幕堆满了很多无用的信息。一般情况下我们需要找到首次出现错误的地方,在gcc中添加编译选项可以使编译停止在第一次出现错误的地方:

$ gcc -Wfatal-errors foo.c		// GCC 4.0 and later
$ g++ -Wfatal-errors foo.cpp
$ g++ -fmax-errors=N foo.cpp	// 在出现第 N 此错误的时候停止编译,GCC 4.6 and later

如果使用cmake构建的项目:

add_definitions(
    -Wfatal-errors
    )
    
或者

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wfatal-errors")  // better

打开警告信息:

$ g++ -Wall foo.cpp		//提示警告信息

警告信息当做错误处理:

$ g++ -Werror foo.cpp		//将警告信息当做错误显示

打开测试浮点数相等的警告:

$ g++ -Wfloat-equal foo.cpp		//通常测试浮点数相等是不准确的

默认情况下,GCC/G++链接时优先链接动态库,如果没有动态库,则链接相应的静态库。同时 -Wl,-Bstatic 选项指定静态链接后面的库,而 -Wl,-Bdynamic 动态链接后面的库。如果指定为 -static,则全部默认链接静态库。

-Werror, -Wimplict-function-declaration:打开不明确函数声明的警告,并将所有警告设置为error。
-Werror=implict-function-declaration:只将不明确函数声明的警告设置为error。
-Wall:打开所有的警告信息。
-Wunused-variable:打开未定义的值的警告。
-w:忽略所有的警告信息。
CC [M] /tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel/nvidia/linux_nvswitch.o make[3]: *** [/tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel/nvidia/libspdm_rsa_ext.o] Error 1 CC [M] /tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel/nvidia/procfs_nvswitch.o cc1: error: -Werror=date-time: no option -Wdate-time make[3]: *** [/tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel/nvidia/nvlink_linux.o] Error 1 CC [M] /tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel/nvidia/i2c_nvswitch.o cc1: error: -Werror=date-time: no option -Wdate-time make[3]: *** [/tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel/nvidia/nvlink_caps.o] Error 1 cc1: error: -Werror=date-time: no option -Wdate-time make[3]: *** [/tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel/nvidia/linux_nvswitch.o] Error 1 cc1: error: -Werror=date-time: no option -Wdate-time make[3]: *** [/tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel/nvidia/procfs_nvswitch.o] Error 1 cc1: error: -Werror=date-time: no option -Wdate-time make[3]: *** [/tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel/nvidia/i2c_nvswitch.o] Error 1 make[3]: Target `__build' not remade because of errors. make[2]: *** [/tmp/selfgz122201/NVIDIA-Linux-x86_64-550.90.12/kernel] Error 2 make[2]: Target `modules' not remade because of errors. make[1]: *** [__sub-make] Error 2 make[1]: Target `modules' not remade because of errors. make[1]: Leaving directory `/usr/src/kernels/5.10.38-21.hl02.el7.x86_64' make: *** [modules] Error 2 还是有这些报错
06-06
root@4a39b502e13d:/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface# make gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.12' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) make -C /nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117 M=/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface modules make[1]: Entering directory '/nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117' make[2]: Entering directory '/nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117' CC [M] /nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface/tp_rc.o cc1: error: plugin support is disabled; configure with --enable-plugin cc1: error: plugin support is disabled; configure with --enable-plugin cc1: error: plugin support is disabled; configure with --enable-plugin cc1: error: unrecognized command line option '-Wno-typedef-redefinition' [-Werror] cc1: all warnings being treated as errors scripts/Makefile.build:273: recipe for target '/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface/tp_rc.o' failed make[3]: *** [/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface/tp_rc.o] Error 1 Makefile:2030: recipe for target '/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface' failed make[2]: *** [/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface] Error 2 make[2]: Leaving directory '/nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117' makefile:22: recipe for target 'modules' failed make[1]: *** [modules] Error 2 make[1]: Leaving directory '/nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117' Makefile:27: recipe for target 'all' failed make: *** [all] Error 2 gcc -v显示--enable-plugin,为什么还会报错呢?
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值