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 [2020/03/06 19:50] – Ajout de "Risque du coding style (espace avant parenthèse)" 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=== |
| - | <code c> | + | |
| - | #define STR_HELPER(x) #x | + | {{gh>https:// |
| - | #define STR(x) STR_HELPER(x) | + | |
| - | </code> | + | |
| Exemple d' | Exemple d' | ||
| - | <code c> | + | {{gh>https:// |
| - | #define GCCVERSION STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__) | + | |
| - | </code> | + | Rendu : |
| + | {{gh>https:// | ||
| [[http:// | [[http:// | ||
| - | ====Ecrire un commentaire dans une macro multi lignes==== | + | ===Ecrire un commentaire dans une macro multi lignes=== |
| <code c> | <code c> | ||
| #define SOME_BIG_MACRO(input)\ | #define SOME_BIG_MACRO(input)\ | ||
| Ligne 19: | Ligne 19: | ||
| </ | </ | ||
| - | ====Risque du coding style (espace avant parenthèse)==== | + | ===Forcer le ; après une macro=== |
| - | 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. | + | |
| + | 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; }}} | ||
| - | int main() | + | if (*p != 0) |
| - | { | + | |
| - | | + | else |
| - | // Interprétation erronée à cause de l' | + | |
| - | // (ARG1, ARG2) ARG1 + ARG2s(1, 2); | + | |
| - | } | + | |
| </ | </ | ||
| + | |||
| + | 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.1583520624.txt.gz · Dernière modification : de root
