Extern union

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • premchandmp
    New Member
    • May 2007
    • 2

    #1

    Extern union

    Hi
    I have a header file in which a union is defined .
    Code:
    union {
     Uint32 uint;
     short channel[2];
     } AIC_data;
    I want to declare it to be extern so as to avoid multiple definitions while the header file is included more than once by the source files in my project which i am doing using Code Composer Studio(which has C,C++ compiler).
    When i declare it to be extern the error message showing is "Storage class cannot be specified here".I would like to know the syntax for doing it.
    Any help would be appreciated.
    prem
    Last edited by JosAH; May 2 '07, 05:48 PM. Reason: added [code] ... [/code] tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Probably the best (or easiest) thing would be to split the type definition and the data declaration and definition.

    You should declare the type and the data in the header file and then define the data in a c/cpp file.

    Code:
    // Header
    union MyUnion {
     Uint32 uint;
     short channel[2];
     }; 
    
    extern union MyUnion AIC_data;
    
    // Code file
    union MyUnion AIC_data;

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by premchandmp
      Hi
      I have a header file in which a union is defined .
      Code:
      union {
       Uint32 uint;
       short channel[2];
       } AIC_data;
      I want to declare it to be extern so as to avoid multiple definitions while the header file is included more than once by the source files in my project which i am doing using Code Composer Studio(which has C,C++ compiler).
      When i declare it to be extern the error message showing is "Storage class cannot be specified here".I would like to know the syntax for doing it.
      Any help would be appreciated.
      prem
      I normally use typedefs for readability reasons. This is how I do these things:
      1) define the type in a foo.h file and declare a global variable of that type:
      Code:
      #ifndef _FOO_H_
      #define _FOO_H_
      typedef union {
       Uint32 uint;
       short channel[2];
       } my_union_t; 
      ...
      extern my_union_t AIC_data;
      #endif
      2) then in a foo.c file I define the thing:
      Code:
      #include "foo.h"
      ...
      my_union_t AIC_data;
      3) and in every other .c file that needs that global union:
      Code:
      #include "foo.h"
      kind regards,

      Jos

      Comment

      • premchandmp
        New Member
        • May 2007
        • 2

        #4
        Thanks Banfa,
        your advice helped me to solve the problem
        prem




        Originally posted by Banfa
        Probably the best (or easiest) thing would be to split the type definition and the data declaration and definition.

        You should declare the type and the data in the header file and then define the data in a c/cpp file.

        Code:
        // Header
        union MyUnion {
         Uint32 uint;
         short channel[2];
         }; 
        
        extern union MyUnion AIC_data;
        
        // Code file
        union MyUnion AIC_data;

        Comment

        Working...