This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

about extern struct question.

dear all, i defind a struct in a head file and other file will be use this struct , i use keyword extern but always has warning or error message , please tell me really method. thanks a lot.

main.c
----------------
typedef union{
        unsigned char Byte;
        struct{
                unsigned _40:1; //Cmd 0
            unsigned _41:1;     //Acmd 41
            unsigned _50:1;     //Cmd 16
            unsigned _49:1;     //Cmd 9
            unsigned b4:1;
            unsigned b5:1;
            unsigned b6:1;
            unsigned b7:1;
        }Audio;
        struct{
                unsigned _51:1; //Cmd 17 read_single_block
            unsigned _58:1;     //Cmd 24 write_single_block
            unsigned xx1:1;     //Cmd 16
            unsigned xx2:1;     //Cmd 9
            unsigned xb4:1;
            unsigned xb5:1;
            unsigned xb6:1;
            unsigned xb7:1;
        }Bits;
}_Data;

_Data init_flag;

other.c
----------------------------------
extern struct _Data init_flag;

Parents Reply Children
  • Have you placed the declarations (including the extern declaration) in the header file?

    Have you included the header file from all source files that need to access the variable or data type?

    Have you instantiated the variable in one - and exactly one - source file?

  • dear sir,
    i follow the Per Westermark method, but still has warning message "*** WARNING L1: UNRESOLVED EXTERNAL SYMBOL"
    Would You know what's happen ? thanks a lot.

  • my_struct.h
    ------------------
    typedef union{
            unsigned char Byte;
            struct{
                    unsigned _40:1; //Cmd 0
                unsigned _41:1;     //Acmd 41
                unsigned _50:1;     //Cmd 16
                unsigned _49:1;     //Cmd 9
                unsigned b4:1;
                unsigned b5:1;
                unsigned b6:1;
                unsigned b7:1;
            }Audio;
            struct{
                    unsigned _51:1; //Cmd 17 read_single_block
                unsigned _58:1;     //Cmd 24 write_single_block
                unsigned xx1:1;     //Cmd 16
                unsigned xx2:1;     //Cmd 9
                unsigned xb4:1;
                unsigned xb5:1;
                unsigned xb6:1;
                unsigned xb7:1;
            }Bits;
    }_Data;
    
    extern _Data init_flag;
    
    
    main.c
    ----------------
    #include "my_struct.h"
    
    .....
    .....
    init_flag.Audio._40=1;
    ...........
    
    *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL
    *** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL
    

  • In _one_ of your source files, add a line:

    _Data init_flag;

    The "extern" lines only informs the compiler that somewhere there is a variable init_flag. You must follow up by actually allocating the variable somewhere too.

  • thanks a lot .
    i extern the struct success.