Making GCC and Other C++ Compilers Very Strict

2019-03-10 05:43发布

I'm working on a large collaborative C++ project that is both developed and run on various flavors of Linux, OS X and Windows. We compile across these platforms with GCC, Visual Studio C++ and the Intel C++ compiler. As more and more people start developing code for the project, we're starting to see weird errors in compilation and runtime that are specific to particular compilers on particular operating systems. An example of this is implicit inclusion of headers that certain OS/compiler pairs seem to find for you, accidentally overloading a function from a base class in a derived class.

My goal is to make compilation on GCC more strict and catch more errors across all platforms so that we don't keep running into these problems. Here's my list of flags that I'm thinking about trying out for GCC that I've found via Google and the GCC man pages:

  • -Wall
  • -Wextra
  • -Winit-self
  • -Wold-style-cast
  • -Woverloaded-virtual
  • -Wuninitialized
  • -Wmissing-declarations
  • -Winit-self
  • -ansi
  • -pedantic

What are the other flags that people use to make GCC (and less importantly Visual Studio C++ and the Intel C++ Compiler) obey a stricter standard of the C++ language? Be specific about which compiler and version you're talking about, as some of these might not be implemented in all versions of all compilers.

7条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-10 06:07

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

查看更多
The star\"
3楼-- · 2019-03-10 06:12

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

查看更多
萌系小妹纸
4楼-- · 2019-03-10 06:13

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.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-03-10 06:19

-pedantic-errors.

See more on gcc(1).

查看更多
一纸荒年 Trace。
6楼-- · 2019-03-10 06:21

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/

查看更多
成全新的幸福
7楼-- · 2019-03-10 06:23

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.)

查看更多
登录 后发表回答