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

Extern struct

Hi all:

I am trying to use data from a structure in different modules ( extern struct), with no success. Here's what I am doing:

main.c:

#include "main.h"
struct my_struct new_struct;

void main(void)
{
   new_struct.a = 4321;
}

file1.c:

#include "main.h"
extern my_struct new_struct;


void test()
{
    new_struct.a = 1234;
}

main.h:


struct my_struct
{
    int a;
    int b;
}


The compiler tells me that left side of . requires struct or union....
As I have followed previous threads about this, I see this guidelines are correct. What can be wrong here ?
I am using c51 ver 8.12

Thanks in advance

Juan


  • I seem to remember a bug in the compiler at around the version you're using which was like the problem you describe. Though I've just been back through the release note history and cannot find a mention of it.

    Try updating to the latest version.

  • You omitted the struct keyword in your code. You should write

    extern struct my_struct new_struct;
    


    rather than

    struct my_struct new_struct;
    

  • Sorry, it's

    extern struct my_struct new_struct;
    


    rather than

    extern my_struct new_struct;
    

  • Thanks Mike for your info. I checked and looks like a compiler bug. I did the same with another compiler and works fine. Unfortunately the first year is over and no need to buy the yearly service just for that.

    Thanks

  • "I checked and looks like a compiler bug. I did the same with another compiler and works fine."

    The other compiler has a bug.

  • I checked and looks like a compiler bug.

    What I've seen in this thread leads me to believe that there is no bug.
    If a compiler doesn't complain about the missing struct keyword, it probably means that it's a C++ compiler or it is configured to accept syntax deviating from the C standard. It doesn't mean that Keil's compiler has a bug.

  • I've been back through the documentation.

    The problem that I was thinking about was fixed in version 8.06 (although the description present in the release notes does not quite cover the full situation).

    Sorry for adding confusion to an otherwise simple problem.

  • IB Shy:
    Are you suggesting the compiler is fine, and it's just a setting ? If so is the case, what setting could it be ? As I said, I tested it in another compiler (Visual c++) and works fine. Therefore I assumed a problem with the Keil's one.
    Give me more light please.
    Thanks
    Juan

    1. With new_struct's declaration in file1.c lacking the keyword 'struct', it is not a structure declaration, it's an 'int' declaration.
    2. C51 produced an error message telling you what types the '.' operator works with (none are 'int').
    3. You used Visual C++ to compile your example without error.
    4. Successful compilation with Visual C++ led you to believe the problem was C51's bug rather than it a problem with your declaration.

    Some observations:

    • There is no setting to have C51 accept valid C, but C that you did not intend.
    • Certainly it would be less effort to have carefully examined the error message and correct the declaration by inserting "struct" between "extern" and "my_struct" than to try to find a means of having C51 change its concept of standard C's "default int" behavior.
    • If you are going to use a C++ compiler to validate C code, you should be investigating options for it, not C51 -- options that put in a strictly C-conforming mode.
    • Other options include using lint.

    For example, I've pasted the following into Gimpel's online lint gimpel-online.com/.../genPage.py :

    struct my_struct
    {
        int a;
        int b;
    };
    
    extern my_struct new_struct;
    
    void test()
    {
        new_struct.a = 1234;
    }
    

    With these results:

    FlexeLint for C/C++ (Unix) Vers. 9.00c, Copyright Gimpel Software 1985-2009
    
    --- Module:   diy.c (C)
                     _
    extern my_struct new_struct;
    diy.c  10  Info 808: No explicit type given symbol 'my_struct',
        int assumed
    diy.c  10  Error 10: Expecting ';'
    diy.c  10  Error 19: Useless Declaration
        _
        new_struct.a = 1234;
    diy.c  14  Error 40: Undeclared identifier 'new_struct'
    diy.c  14  Error 10: Expecting a structure or union
    diy.c  14  Error 40: Undeclared identifier 'a'
    diy.c  14  Error 63: Expected an lvalue
    
        --- Wrap-up for Module: diy.c
    
    Info 753: local struct 'my_struct' (line 4, file diy.c) not
        referenced
    diy.c  4  Info 830: Location cited in prior message
    Info 754: local structure member 'my_struct::a' (line 6, file
        diy.c) not referenced
    diy.c  6  Info 830: Location cited in prior message
    Info 754: local structure member 'my_struct::b' (line 7, file
        diy.c) not referenced
    diy.c  7  Info 830: Location cited in prior message
    
    --- Global Wrap-up
    
    Info 714: Symbol 'test(void)' (line 12, file diy.c) not
        referenced
    diy.c  12  Info 830: Location cited in prior message