IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Autres éditeurs Discussion :

define avec GCC.


Sujet :

Autres éditeurs

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 15
    Points : 13
    Points
    13
    Par défaut define avec GCC.
    Bonjour,

    J'ai etudie une partie du code de Linux et certaines
    definitions sont encapsulee dans do{.....}while(0)

    exemple:

    #define spin_lock_init(x) do { (x)->lock = 0; } while(0)

    pourquoi ???

    merci

  2. #2
    zul
    zul est déconnecté
    Membre éclairé Avatar de zul
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    498
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 498
    Points : 699
    Points
    699
    Par défaut
    Ceci est tiré de www.kernelnewbies.org

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
     
    There are a couple of reasons: 
    (from Dave Miller) Empty statements give a warning from the compiler so this is why you see #define FOO do { } while(0). 
    (from Dave Miller) It gives you a basic block in which to declare local variables. 
    (from Ben Collins) It allows you to use more complex macros in conditional code. Imagine a macro of several lines of code like:
     
     
    #define FOO(x) \
            printf("arg is %s\n", x); \
            do_something_useful(x);
    Now imagine using it like:
     
     
            if (blah == 2)
                    FOO(blah);
     
    This interprets to:
     
     
            if (blah == 2)
                    printf("arg is %s\n", blah);
                    do_something_useful(blah);;
    As you can see, the if then only encompasses the printf(), and the do_something_useful() call is unconditional (not within the scope of the if), like you wanted it. So, by using a block like do{...}while(0), you would get this:
     
     
            if (blah == 2)
                    do {
                            printf("arg is %s\n", blah);
                            do_something_useful(blah);
                    } while (0);
    Which is exactly what you want. 
    (from Per Persson) As both Miller and Collins point out, you want a block statement so you can have several lines of code and declare local variables. But then the natural thing would be to just use for example:
     
     
      #define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
    However that wouldn't work in some cases. The following code is meant to be an if-statement with two branches:
     
     
      if(x>y)
        exch(x,y);          // Branch 1
      else  
        do_something();     // Branch 2
    But it would be interpreted as an if-statement with only one branch:
     
     
      if(x>y) {                     // Single-branch if-statement!!!
        int tmp;            // The one and only branch consists
        tmp = x;            // of the block.
        x = y;
        y = tmp;
      }
      ;                             // empty statement
      else                  // ERROR!!! "parse error before else"
        do_something();
    The problem is the semi-colon (;) coming directly after the block.
     
    The solution for this is to sandwich the block between do and while(0). Then we have a single statement with the capabilities of a block, but not considered as being a block statement by the compiler.
     
    Our if-statement now becomes:
     
     
      if(x>y)
        do {
          int tmp;
          tmp = x;
          x = y;
          y = tmp;
        } while(0);
      else
        do_something();
    ZUL

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 15
    Points : 13
    Points
    13
    Par défaut
    cool, merci pour ta reponse claire et precise !

    j'en profite pour te (vous) demander ou je pourrais trouver une doc
    sur les principes d'utilisation des fonctions inline avec gcc.

    En effet, toujours dans le code de Linux, on peut voir
    - des static inline fonction() dans des .h
    - des extern inline fonction()

    merci

  4. #4
    zul
    zul est déconnecté
    Membre éclairé Avatar de zul
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    498
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 498
    Points : 699
    Points
    699
    Par défaut
    sur le meme site que cité précédemment. Dans la section FAQ.

    ZUL

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 15
    Points : 13
    Points
    13
    Par défaut
    desole !

    j'ai survole le site mais j'avais pas vu !

    merci

Discussions similaires

  1. Comment linker avec odbc32.lib dans le code ? (avec gcc)
    Par _Audrey_ dans le forum Autres éditeurs
    Réponses: 3
    Dernier message: 06/11/2008, 17h40
  2. compilation avec gcc : erreurs bizard !!!
    Par CodeurNé dans le forum C
    Réponses: 2
    Dernier message: 23/09/2005, 18h09
  3. Forcer 'inline' avec GCC ou ICC
    Par Charlemagne dans le forum Autres éditeurs
    Réponses: 4
    Dernier message: 12/05/2005, 18h22
  4. les .a avec gcc
    Par Groove dans le forum Autres éditeurs
    Réponses: 4
    Dernier message: 31/03/2003, 07h59
  5. getch() avec gcc
    Par Jorus dans le forum Autres éditeurs
    Réponses: 5
    Dernier message: 16/12/2002, 14h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo