C语言-Werror=sign-compare编译错误解决方案

在做C语言的一个小测试时,写了下面的一段代码,涉及到有符号数和无符号数的大小比较。

// automatic type conversion
if (-1L < 1U)
{
        printf("sizeof(unsigned int)=%lu\nsizeof(signed long int)=%lu\n",
               sizeof(unsigned int), sizeof(signed long int));
        printf("\'-1L<1U\' since unsigned int is promoted to signed long type!\n");
}
if (-1L > 1UL)
{
        printf("sizeof(signed long int)=%lu\nsizeof(unsigned long int)=%lu\n",
               sizeof(signed long int), sizeof(unsigned long int));
        printf("\'-1L>1UL\' since signed long int is promoted to unsigned type!\n");
}

然后使用CMake编译时显示错误

/home/cv/playground_test/src/playground.cc:185:10: In function‘int main(int, const char**)’:
error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
  if (-1L > 1UL)
      ~~~~^~~~~
cc1plus: all warnings being treated as errors
CMakeFiles/playground.dir/build.make:62: recipe for target 'CMakeFile
s/playground.dir/src/playground.c.o' failed

有符号数与无符号数的比较,虽然有时确实需要特别注意,但也不至于上来就Fatal Error,尤其当我们明确需要这样操作时。

-Werror是gcc/g++的配置参数,执行严格的错误检查,这里显然是把一般的警告也作为错误对待了,在CMakeLists中将其去掉即可。

选项 说明
-Wall 打开一些必要的警告选项,建议编译时加此选项。
-Werror 视警告为错误,出现任何警告即放弃编译。
-Wextra 打印一些额外的警告信息。
-w 禁止显示所有警告信息。

把如下展示的第11行

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")

替换成

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

然后重新编译就不会报错了。

修改后的CMakeLists.txt文件内容如下

01 cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
02 project(playground)
03
04 #====================================================
05 ## C++ compiler options
06 #====================================================
07 set(CMAKE_CXX_STANDARD 11)
08 set(CMAKE_CXX_EXTENSIONS OFF)
09 set(CMAKE_CXX_STANDARD_REQUIRED ON)
10 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall")
11 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
12 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
13
14 set(CMAKE_BUILD_TYPE RELEASE)
15 # Set output directory to store executables, create if not exist
16 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
17
18 ## Search for source files
19 set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
20 file(GLOB_RECURSE SRC_LIST "${SRC_DIR}/[^.]*.c*")
21
22 #====================================================
23 ## Compile target executables
24 #====================================================
25 foreach(src ${SRC_LIST})
26         string(REGEX REPLACE ".+/(.+)\\..*" "\\1" SRC_FILE_NAME ${src})
27         add_executable(${SRC_FILE_NAME} ${src})
28         set_target_properties(${SRC_FILE_NAME} PROPERTIES LINKER_LANGUAGE CXX)
29         target_link_libraries(${SRC_FILE_NAME} pthread)
30 endforeach(src ${SRC_LIST})

工程目录结构和运行主机配置如下。

# Project tree structure
.
├── CMakeLists.txt
├── bin
│   └── playground
├── build
└── src
    └── playground.c

# Linux Ubuntu system information
cv@cv:~$ uname -a
Linux cv 4.18.0-25-generic #26~18.04.1-Ubuntu SMP Thu Jun 27 07:28:31 UTC 2019
x86_64 x86_64 x86_64 GNU/Linux

cv@cv:~$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

cv@cv:~$ cmake --version
cmake version 3.10.2
CMake suite maintained and supported by Kitware (kitware.com/cmake)

cv@cv:~$ make --version
GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

这里只是摘出相关的部分进行记录和说明,完整测试代码及环境要求详见 https://github.com/philleer/leetcode_test/tree/dev

(全文完)


本文作者 :phillee
发表日期 :2022年01月16日
本文链接https://www.cnblogs.com/phillee/p/15810793.html
版权声明 :自由转载-非商用-非衍生-保持署名(创意共享3.0许可协议/CC BY-NC-SA 3.0)。转载请注明出处!
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。

posted @ 2022-01-16 17:56  coffee_tea_or_me  阅读(2484)  评论(0编辑  收藏  举报