469,602 Members | 1,726 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Extern union

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
3 11677
Banfa
9,064 Expert Mod 8TB
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
JosAH
11,448 Expert 8TB
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
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.

Similar topics

2 posts views Thread by ik | last post: by
10 posts views Thread by Mark A. Gibbs | last post: by
6 posts views Thread by Neil Zanella | last post: by
4 posts views Thread by dyu9999 | last post: by
19 posts views Thread by ccwork | last post: by
5 posts views Thread by siliconwafer | last post: by
17 posts views Thread by Tapeesh | last post: by
5 posts views Thread by Christian Christmann | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.