lang:c:preprocesseur
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
lang:c:preprocesseur [2017/09/09 13:48] – maff => mhtml root | lang:c:preprocesseur [2024/04/09 15:21] (Version actuelle) – Ajout de "Forcer le ; après une macro" root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | =====Convertir un nombre en chaîne de caractères===== | + | ===Convertir un nombre en chaîne de caractères=== |
+ | |||
+ | {{gh> | ||
+ | |||
+ | Exemple d' | ||
+ | {{gh> | ||
+ | |||
+ | Rendu : | ||
+ | {{gh> | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | ===Ecrire un commentaire dans une macro multi lignes=== | ||
<code c> | <code c> | ||
- | # | + | # |
- | #define STR(x) STR_HELPER(x) | + | |
+ | SOME_OTHER_FUNCTION_CALL() | ||
</ | </ | ||
- | Exemple d'utilisation : | + | ===Forcer le ; après une macro=== |
+ | |||
+ | Chaque ligne de code doit se terminer par un '';'' | ||
+ | |||
+ | Cela peut poser problème avec certaines macros. | ||
+ | |||
+ | * Deux lignes de code | ||
<code c> | <code c> | ||
- | # | + | # |
+ | { char *lim = (limit); \ | ||
+ | while (p < lim) { \ | ||
+ | if (*p++ != ' ') { \ | ||
+ | p--; break; }}} | ||
+ | |||
+ | if (*p != 0) | ||
+ | SKIP_SPACES (p, lim); | ||
+ | else | ||
</ | </ | ||
- | [[http:// | + | Problème : il y a un '';'' |
+ | |||
+ | Solution : '' | ||
+ | |||
+ | <code c> | ||
+ | #define SKIP_SPACES(p, | ||
+ | do { char *lim = (limit); | ||
+ | while (p < lim) { \ | ||
+ | if (*p++ != ' ') { \ | ||
+ | p--; break; }}} \ | ||
+ | while (0) | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | * Une fonction complète | ||
+ | |||
+ | Problème : | ||
+ | |||
+ | <code c> | ||
+ | #define MACRO() \ | ||
+ | void foo(){} | ||
+ | </ | ||
+ | |||
+ | Solution : '' | ||
+ | |||
+ | <code c> | ||
+ | #ifdef __cplusplus | ||
+ | #include < | ||
+ | #else | ||
+ | #include < | ||
+ | #endif | ||
+ | |||
+ | #define MACRO() \ | ||
+ | void foo(){} \ | ||
+ | static_assert(true, | ||
+ | |||
+ | MACRO(); | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ===Détection du compilateur=== | ||
+ | |||
+ | Note : sous '' | ||
+ | |||
+ | <code cpp> | ||
+ | #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) | ||
+ | #pragma GCC diagnostic push | ||
+ | #pragma GCC diagnostic ignored " | ||
+ | #endif | ||
+ | |||
+ | #ifdef __clang__ | ||
+ | #pragma clang diagnostic push | ||
+ | #pragma clang diagnostic ignored " | ||
+ | #endif | ||
+ | |||
+ | #ifdef _MSC_VER | ||
+ | #pragma warning(push) | ||
+ | #pragma warning(disable : 4242) | ||
+ | #endif | ||
+ | |||
+ | ... | ||
+ | |||
+ | #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) | ||
+ | #pragma GCC diagnostic pop | ||
+ | #endif | ||
+ | |||
+ | #ifdef __clang__ | ||
+ | #pragma clang diagnostic pop | ||
+ | #endif | ||
+ | |||
+ | #ifdef _MSC_VER | ||
+ | #pragma warning(pop) | ||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ===Détection de l' | ||
+ | |||
+ | * Windows : '' | ||
+ | * Windows 64 bit : '' | ||
+ | * Linux : '' | ||
+ | * Android : '' | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | Cas courant : | ||
+ | |||
+ | <code c> | ||
+ | #if defined(_WIN32) | ||
+ | #elif __APPLE__ | ||
+ | #elif __ANDROID__ | ||
+ | #elif __linux__ | ||
+ | #else | ||
+ | # error " | ||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | Note : Apple définit également '' | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ===Risque du coding style (espace avant parenthèse)=== | ||
+ | |||
+ | Si on souhaite faire passer des arguments à la macro, il est nécessaire que la parenthèse touche le nom de la macro. Sinon, le préprocesseur interprétera la parenthèse comme le début du remplacement. | ||
+ | |||
+ | Exemple : | ||
+ | |||
+ | {{gh> | ||
+ | |||
+ | Rendu : | ||
+ | |||
+ | {{gh> | ||
+ | |||
+ | Heureusement, |
lang/c/preprocesseur.1504957717.txt.gz · Dernière modification : 2017/09/09 13:48 de root