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

error C202 undefined identifier

Hi,

I defined,

Uint16 TCounts[10]; in globals.h

and tried to use it in cc01drv.c where #include "globals.h" is there.

but during build the error msg coming up is, "C202 undefined identifier"

Why this is happening, I haven't initialize the array.

Parents
  • No! You don't!

    You need to define the variable exactly once, and then have a declaration visible wherever the variable is used.

    In other words, your project must have one (and only one) definition and may have many declarations.

    "I need to understand it furthur details later."

    No; you really need to understand this now - as it is absolutely fundamental & essential to any project using multiple sources!
    If you don't understand it now, you will soon fall into more traps!

    Note that this is standard 'C' stuff - nothing specifically to do with Keil or embedded - therefore any decent 'C' textbook will cover this!

    eg, see: c-faq.com/.../decldef.html

    "But now I am facing other issue"

    As it's another issue, start another thread!

    You can reference this thread (copy-and-paste a link) if it seems appropriate...

Reply
  • No! You don't!

    You need to define the variable exactly once, and then have a declaration visible wherever the variable is used.

    In other words, your project must have one (and only one) definition and may have many declarations.

    "I need to understand it furthur details later."

    No; you really need to understand this now - as it is absolutely fundamental & essential to any project using multiple sources!
    If you don't understand it now, you will soon fall into more traps!

    Note that this is standard 'C' stuff - nothing specifically to do with Keil or embedded - therefore any decent 'C' textbook will cover this!

    eg, see: c-faq.com/.../decldef.html

    "But now I am facing other issue"

    As it's another issue, start another thread!

    You can reference this thread (copy-and-paste a link) if it seems appropriate...

Children
  • Many thanks. I just gave a quick look at the chapter 9: "The Preprocessor" of TurboC/C++ the Complete reference by Herbert Schildt. But I haven't found what I was looking for.

    My program main.c listed,
    #define _MAIN_
    in the first line and globals.h used,

    #ifdef _MAIN_
    volatile Uint16 xdata PCounts;
    #else
    extern volatile Uint16 xdata PCounts;
    #endif

    What all these meant? Why I have to define main in this way? Is it for C51 uC or, the Keil compiler need to know which file have main function?

    What is meant by #ifdef _MAIN_? Yes, it is defined in main.c so, if the compiler take decleration under it then it don't get the essential "extern", then how it will work.

  • You don't need to define any _MAIN_.

    You have found some sample program where a specific developer have selected to use a specific way to write code. That doesn't mean that a general purpose book will have info about this specific method.

    It isn't part of the preprocessor but part of the compiler to keep track of your definitions and declarations.

    You can write in one (actually many header files, but you should only write it in one) header file:

    extern volatile Uint16 xdata PCounts;
    

    That means that any C file that includes this header fille will know about the existence of PCounts, and what type it takes.

    You can then write in one (exactly one) C file:

    volatile Uint16 xdata PCounts;
    


    This since line is the one that will make the linker reserve room for the variable.

    For maximum security, you should make sure that this since C file also includes the header file that contains the "extern" line, so the compiler can catch if you have managed any type errors between the two lines.

    The use of the preprocessor with _MAIN_ is just some form of syntactic sugar, where a developer has moved both lines into the header file, and then uses the _MAIN_ symbol to get the preprocessor to extract the extern declarations for all source files but the single file where _MAIN_ is defined. And the source file with _MAIN_ defined (possibly a file named main.c) will instead get the line that doesn't contain "extern".

    It would have been better if the code had been written:

    my_header.h:

    extern volatile Uint16 xdata PCounts;
    
    #ifdef _MAIN_
    volatile Uint16 xdata PCounts;
    #endif
    

    and main.c:

    #define _MAIN_
    #include "my_header.h"
    ...
    

    and other source files:

    #include "my_header.h"
    ...
    

    The use of #else in your example means that the compiler will not catch type errors.

  • That would be a book specifically about (Borland's?) Turbo C/C++ - it may well assume that you understand the 'C' programming language in general?

    Because it is explicitly stated to be specific to Turbo C/C++, you need to take care to think about whether the things it says are applicable to Keil (or any other compiler).

    What you really need is a general 'C' textbook - one which describes the language itself, not one specific implementation!

  • reference by Herbert Schildt.

    Bad idea. You picked the worst possible author to acquire a C book by. Dump it now before you do yourself further harm, and get a proper textbook. See the C FAQ for suggestions.

    Why I have to define main in this way?

    Because you don't. You're being taught wrong techniques by a long since outdated book by a thoroughly misguided author.