Making GCC and Other C++ Compilers Very Strict

ⅰ亾dé卋堺 提交于 2019-12-02 20:34:04

Beside the pedantic-error that everyone else suggested, IMO, it's always good to run lint as part of your compile process.

There are some tools out there:

They will save a lot of your time.

You can make pedantic warnings into errors with -pedantic-errors. This will prevent developers from ignoring it. For that matter you could make all warnings into errors as well with -Werror although that can be counter productive in some cases (maybe not in yours though).

Overall, I think, as far as adhering to a strict standard goes, the -pedantic options are the most helpful.

-pedantic-errors.

See more on gcc(1).

Copy and paste below line into your master cmake file. below line comprises almost most useful compiler flags in order to test yourself more stricter.

set(CMAKE_CXX_FLAGS "-O0 -fno-elide-constructors -pedantic-errors -ansi -Wextra -Wall     -Winit-self -Wold-style-cast -Woverloaded-virtual -Wuninitialized -Wmissing-declarations    -Winit-self -std=c++98")

If you dont use cmake just copy flags that in double quotes and send to your compiler

As well as -pendantic you should also provide a -std switch. If you need a stricter compile then you should know what standard you are trying to conform to. Typically for current c++ this would be -std=c++98. ( -ansi performs a similar function in C++ mode, but -std= is more explicit.)

In similar situation we gave up and moved to ACE framework, hiding the difference between platforms.

I wrote the blog post on this topic after researching several options. You also need to handle the cases where you are using other libraries but they are not following strict compilation. Fortunately there is easy way to handle them as well. I have been using this extensively in all my projects.

In short, use following compiler options to turn on very strict mode (below is what I put in CMakeLists.txt):

set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra  -Wstrict-aliasing -pedantic -fmax-errors=5 -Werror -Wunreachable-code -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Wno-unused -Wno-variadic-macros -Wno-parentheses -fdiagnostics-show-option ${CMAKE_CXX_FLAGS}")

You can read more about how to turn on and off this strict mode for specific portions of code here: http://shitalshah.com/p/how-to-enable-and-use-gcc-strict-mode-compilation/

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