Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam

Webinar: Evaluation - 05.12

>
>
>
Compiler warnings

Compiler warnings

Apr 05 2013

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.

Suppression of compiler warnings

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.

Suppression in code

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.

Suppression in compiler/build system

Sometimes editing the code can be problematic when warnings are still there. In such a case, you can try the following options.

Use compilation flags

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.

Mark directory as system directory

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

Popular related articles


Comments (0)

Next comments next comments
close comment form