473,382 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 12919
Banfa
9,065 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

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

Similar topics

2
by: ik | last post by:
Hello all, Under C++ when 'extern' is used with out the string literal "C" does it act the same as a 'static' ? When is it appropriate using 'extern' ? Thanks ~Ik
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
4
by: dyu9999 | last post by:
Hi, I have a following error and would appreciate any help. ======================================================== * in foo.c I have typedef struct _my_data_t { int a;
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
5
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.