Avoid warnings from system libraries/includes using CMake

匆匆过客 提交于 2021-02-11 12:05:36

问题


I have a large base of code and use CMake 3.19.3 to build the compile environment. I keep the code compiling in GCC (9.3), CLang (11), and ICC (19.1). However, for ICC to work I need to remove a lot of warnings from the mandatory checks. My CMake config file looks like this:

if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
    list(APPEND CMAKE_CXX_FLAGS "-no-ansi-alias")
    list(APPEND CMAKE_CXX_FLAGS "-Wno-error=missing-prototypes")
    list(APPEND CMAKE_CXX_FLAGS "-wd68")   # integer conversion resulted in a change of sign
    list(APPEND CMAKE_CXX_FLAGS "-wd111")  # statement is unreachable
    list(APPEND CMAKE_CXX_FLAGS "-wd186")  # pointless comparison of unsigned integer with zero
    list(APPEND CMAKE_CXX_FLAGS "-wd593")  # variable was set but never used
    list(APPEND CMAKE_CXX_FLAGS "-wd654")  # overloaded virtual function is only partially overridden in class
    list(APPEND CMAKE_CXX_FLAGS "-wd1476") # field uses tail padding of a base class
    list(APPEND CMAKE_CXX_FLAGS "-wd1505") # size of class is affected by tail padding
    list(APPEND CMAKE_CXX_FLAGS "-wd1572") # floating-point equality and inequality comparisons are unreliable
    list(APPEND CMAKE_CXX_FLAGS "-wd2196") # routine is both "inline" and "noinline"
    list(APPEND CMAKE_CXX_FLAGS "-wd2415") # variable "xxx" of static storage duration was declared but never referenced
    list(APPEND CMAKE_CXX_FLAGS "-wd2338") # this switch statement does not have a default clause
    list(APPEND CMAKE_CXX_FLAGS "-wd3884") # missing initializer for field
endif()

(In case you wonder, this is the reason why I append things to CMAKE_CXX_FLAGS instead of using add_compile_options())

I need to do so, otherwise, my code fails because of warnings in the C++ library includes (I've also turned on "-pedantic-errors", "-Wall", "-Werror" and "-Wfatal-errors").

For example, in CentOS 7, using gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3) as the base compiler for ICC, this is the output if I enable the warnings disable in the sample above:

-- Found ccache
-- The C compiler identification is Intel 19.1.0.20200306
-- The CXX compiler identification is Intel 19.1.0.20200306
...
...
...
[ 40%] Building CXX object Utils/src/CMakeFiles/utils_objects.dir/boolean_utils.cpp.o
icc: warning #10193: -vec is default; use -x and -ax to configure vectorization
remark #11074: Inlining inhibited by limit max-size
remark #11074: Inlining inhibited by limit max-total-size
remark #11076: To get full report use -qopt-report=4 -qopt-report-phase ipo
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h(5163) (col. 16): error #2102: violation of ansi-alias rules

I've already marked as SYSTEM all the explicit includes to external libraries to avoid this kind of warnings being thrown, for example:

include_directories(SYSTEM ${Python3_INCLUDE_DIRS})

But I have no idea/haven't found anything related on how to do that directly from CMake for the C++ system libraries themselves (like basic_string.h in my sample), without obviously modifying the generated Makefiles by hand. I found this article, but it's related to what I already do. I also found this possible solution, but also found that even CMake discourages such solution:

"It is also probably a good idea to avoid passing -I/-isystem for directories that the compiler already assumes as default include directories. It keeps thinga cleaner and simple."

A MWE would be this:

test.cpp
#include <iostream>
#include <string>

int main()
{
        std::string a("Text");

        std::cout << a.c_str() << std::endl;

        return 0;
}

Compile with:

icc -o OUT -pedantic-errors -fmax-errors=1 -march=native -Wall -Werror -Wfatal-errors -Wextra -ftree-vectorize -g -Wl,--exclude-libs,ALL -O3 -DNDEBUG -fPIC -fvisibility=hidden -Wstrict-aliasing -std=gnu++17 main.cpp

returns:

icc: warning #10193: -vec is default; use -x and -ax to configure vectorization
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h(5052) (col. 50): error #2102: violation of ansi-alias rules

compilation aborted for main.cpp (code 4)

Any suggestions? Thanks a lot.

来源:https://stackoverflow.com/questions/65795496/avoid-warnings-from-system-libraries-includes-using-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!