How to disable GCC warnings for a few lines of code

后端 未结 9 925
我寻月下人不归
我寻月下人不归 2020-11-22 08:32

In Visual C++, it\'s possible to use #pragma warning (disable: ...). Also I found that in GCC you can override per file compiler flags. How can I do this for \"next line\",

9条回答
  •  深忆病人
    2020-11-22 09:17

    I had same issue with external libraries like ROS headers. I like to use following options in CMakeLists.txt for stricter compilation:

    set(CMAKE_CXX_FLAGS "-std=c++0x -Wall -Wextra -Wstrict-aliasing -pedantic -Werror -Wunreachable-code ${CMAKE_CXX_FLAGS}")
    

    However doing this causes all kind of pedantic errors in externally included libraries as well. The solution is to disable all pedantic warnings before you include external libraries and re-enable like this:

    //save compiler switches
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wpedantic"
    
    //Bad headers with problem goes here
    #include 
    #include 
    
    //restore compiler switches
    #pragma GCC diagnostic pop
    

提交回复
热议问题