Webinar: Evaluation - 05.12
Compiler warnings are compiler messages about suspicious code fragments that may contain errors. Unlike compilation errors, warnings do not interrupt the program building process. They are not errors in terms of the programming language, but they can be program errors. However, many compilers can be configured to stop the compilation process when warnings occur.
Do not ignore the warnings. It is better to fix possible errors even before testing the program. You can search long and hard for an error in the debugger, even though the compiler explicitly tells you about it in one of the warnings. Try to eliminate or minimize all the compiler warnings as you work on a project. The fewer warnings there are, the easier it is to notice and process new ones (see "Broken Windows Theory").
Different compilers issue warnings based on their own static code analysis performed at compile time. Therefore, warnings issued by MSVC, GCC, or Clang may differ. Unfortunately, the compiler can't do a thorough analysis because the compilation must be done quickly. That is why it is better to use specialized static code analysis tools to search for more complex errors in a program.
There are cases when you need to suppress warnings issued by the compiler. This often happens when a programmer uses third-party libraries and enables advanced error output.
Code-level suppression involves making changes to the source code. The specific method of suppression depends on the compiler, but they all rely on the same mechanism — the combination of #pragma push and #pragma pop.
MSVC:
#pragma warning(push)
#pragma warning(disable : %WARN%)
// correct code generating the warning %WARN%
#pragma warning(pop)
Clang:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-W${WARN}"
// correct code generating the warning ${WARN}
#pragma clang diagnostic pop
GCC:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-W${WARN}"
// correct code generating the warning ${WARN}
#pragma GCC diagnostic pop
Note that Clang also supports suppression instructions from GCC.
Sometimes editing the code can be problematic when warnings are still there. In such a case, you can try the following options.
Compilation flags disable certain diagnostics for the whole project at once. You can find lists of all diagnostic identifiers and their names at the end.
MSVC:
Add the /wd%WARN_ID% flag to compile options. Here's an example:
/wd4820
GCC and Clang:
Add the -Wno-${WARN_NAME} flag to compile options. An example:
-Wno-unused-comparison
Note that if you need to suppress warnings only for files from a specific directory (library), use the option below.
Most compilers do not issue warnings for system code. You can mark directory as system to disable warnings that you can't influence. We will only show the most common ways to do it, as it depends on the build system and compiler you use.
CMake:
Use the target_include_directories instruction. The main thing is to add the SYSTEM parameter before the required path. For example, like this:
target_include_directories(my_target SYSTEM /path/to/lib)
MSVC:
You can use the /external compilation flag. To learn more about the flag and how to use it, consult the documentation.
GCC and Clang:
Use the -isystem flag when specifying file search paths. For example:
-isystem /path/to/lib
Sources
0