how to declare a struct as external in header file CW5.1

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to declare a struct as external in header file CW5.1

10,785 Views
stevec
Contributor III
I have set up a simple struct of bits
struct{
    unsigned char flag1:1
    unsigned char flag2:1
    unsigned char flag3:1
}flags;

I have put this in the (main.h) header. How do I make this struct visible to other files. I already include the (main.h) file in the relevant .c files.
I use the #ifdef
                variable1;
                variable2;
                (struct will go in here)
      
                #else
                extern variable1;
                extern variable2;
                (how do I get struct to show itself?)
                #endif
for the form of the header file.
Can I do "extern struct flags;"?
I have tried various things but always get compiler errors
Labels (1)
0 Kudos
5 Replies

534 Views
CompilerGuru
NXP Employee
NXP Employee
In your code snippet, a typedef is missing or the flags is at the wrong place.
So basically that code did define a variable flags, and not a type.
Try this:


Code:
typedef struct flags {
    unsigned char flag1:1
    unsigned char flag2:1
    unsigned char flag3:1
} flags;

or drop the typedef and the last flags

Code:
struct flags {    unsigned char flag1:1    unsigned char flag2:1    unsigned char flag3:1};

 

Daniel
0 Kudos

534 Views
stevec
Contributor III
The code as I wrote it above compiled and I could set and clear flags as bits. I just couldn't get another file to see the structure.
I will try out your suggestion. But how do I get other files to see the declared bits?
0 Kudos

534 Views
CompilerGuru
NXP Employee
NXP Employee
Of course the code compiled and worked, it just did not define a type flags, it defined a variable called flags.

How to split up an application into mutiple modules is explained in every good C book.
Basically define the types in the header, declare the variables in the header and define every object
in one C file. But in order to know what is a type, a declaration or a definition, you still need to read a good C book first Smiley Wink.


In the end, it should look somehow like this:



Code:
/* file: header.h */
#ifndef _HEADER_H_
#define _HEADER_H_

#ifndef __cplusplus
extern "C" {
#endif

typedef struct {
    unsigned char flag1 : 1;
    unsigned char flag2 : 1;
    unsigned char flag3 : 1;
} flags_type;

extern flags_type flags_var;

extern void fun(flags_type* ptr);

#ifndef __cplusplus
}
#endif

#endif /*_HEADER_H_ */

 
Code:
/* file: header.c */#include "header.h" flags_type flags_var;void fun(flags_type* ptr){  /* .. code .. */}

 


0 Kudos

534 Views
bigmac
Specialist III
Hello,
 
Within the previous post, I suspect that the line
#ifndef __cplusplus
 
should actually be
#ifdef __cplusplus
 
at both locations where it is used.
 
Regards,
Mac
 
 
 
0 Kudos

534 Views
CompilerGuru
NXP Employee
NXP Employee
of course, thanks for the fix

Daniel
0 Kudos