Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
Compiler warnings

Compiler warnings

05 Avr 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

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam