423,834 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,834 IT Pros & Developers. It's quick & easy.

Extern union

P: 2
Hi
I have a header file in which a union is defined .
Expand|Select|Wrap|Line Numbers
  1. union {
  2.  Uint32 uint;
  3.  short channel[2];
  4.  } 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
May 2 '07 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
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.

Expand|Select|Wrap|Line Numbers
  1. // Header
  2. union MyUnion {
  3.  Uint32 uint;
  4.  short channel[2];
  5.  }; 
  6.  
  7. extern union MyUnion AIC_data;
  8.  
  9. // Code file
  10. union MyUnion AIC_data;
  11.  
May 2 '07 #2

Expert 10K+
P: 11,448
Hi
I have a header file in which a union is defined .
Expand|Select|Wrap|Line Numbers
  1. union {
  2.  Uint32 uint;
  3.  short channel[2];
  4.  } 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:
Expand|Select|Wrap|Line Numbers
  1. #ifndef _FOO_H_
  2. #define _FOO_H_
  3. typedef union {
  4.  Uint32 uint;
  5.  short channel[2];
  6.  } my_union_t; 
  7. ...
  8. extern my_union_t AIC_data;
  9. #endif
2) then in a foo.c file I define the thing:
Expand|Select|Wrap|Line Numbers
  1. #include "foo.h"
  2. ...
  3. my_union_t AIC_data;
3) and in every other .c file that needs that global union:
Expand|Select|Wrap|Line Numbers
  1. #include "foo.h"
kind regards,

Jos
May 2 '07 #3

P: 2
Thanks Banfa,
your advice helped me to solve the problem
prem




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.

Expand|Select|Wrap|Line Numbers
  1. // Header
  2. union MyUnion {
  3.  Uint32 uint;
  4.  short channel[2];
  5.  }; 
  6.  
  7. extern union MyUnion AIC_data;
  8.  
  9. // Code file
  10. union MyUnion AIC_data;
  11.  
May 3 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.