Thread: variable declaration within a .h file

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    variable declaration within a .h file

    I'm not sure what's wrong with declaring variables only visible to RTCcustom.h and RTCcustom.c files as shown in the highlighted lines below. Any comments would be much appreciated. Also, Merry Christmas to the readers.

    Code:
    // RTCcustom.h                         <[email protected]>
    // Created: Dec 24, 2007
    // Modified: Dec 24, 2007
    // This contains function declarations of Real-Time Clock handling
    // routines on DS1302
    
    #include "typedefs.h"
    
    #ifndef RTCCUSTOM_H
    #define RTCCUSTOM_H
    
    static uchar MIO;        // address of MCU pin controlling the IO pin of the RTC
    static uchar MCE;        // address of MCU pin controlling the CE pin of the RTC
    static uchar MSCLK;      // address of MCU pin controlling the SCLK pin of the RTC
    
    void setRTCpin(uchar IO, uchar CE, uchar SCLK);  // set MCU pins controlling RTC
    uchar MIOreturn(void);         // MIO accessor
    uchar MCEreturn(void);         // MCE accessor
    uchar MSCLKreturn(void);       // MSCLK accessor
    
    #endif

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are you seeing a problem?

    If you stick to the "one .c file includes it" rule, then there won't be a problem, functionally at least. Though it's not a style habit I would suggest you get used to.

    OTOH, because you declared them all static, you could include the .h in several files, and end up with multiple static copies of the variables, and none of them would be shared between the modules. Maybe this is what you want, or maybe it isn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I'm seeing a problem about the compiler complaining about those variable declarations. I'm building several .c and .h files together into one hex file, so I don't think putting everything in one .c file would work quite well in terms of adaptability. I only need one copy of those variables throughout RTCcustom.h and RTCcustom.c. Is there any information that I'm missing to disclose that might help further pinpoint the problem?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, posting actual error messages would be useful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Here is some of the problems mikroC is complaining in this imagebin link: http://imagebin.org/12587

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Doesn't your IDE have the ability for you to copy/paste the text of your error messages rather than pasting screen dumps?

    If you move those 3 lines to just after you've included RTCcustom.h in RTCcustom.c, do you get the same effect?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    My IDE doesn't have the capability unfortunately. I'm getting the same error.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well whenever I see "internal error", that usually means a broken compiler.
    Who knows what it's barfing on, or why.

    Is MIO (for example) declared as something else in another header file. I can see say
    #define MIO 3

    Then that code you posted expanding to
    static uchar 3;
    really causing some problems.

    Does your compiler have the equivalent of the -E parameter supported by gcc?
    That is to just run the pre-processor step and then stop. You can then see what the compiler really sees.

    > My IDE doesn't have the capability unfortunately.
    Since the compiler would normally write to stdout or stderr, there should be a file somewhere, which the IDE then processes for display.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I have changed my RTCcustom.c file as shown:
    Code:
    // RTCcustom.c                         <[email protected]>
    // Created: Dec 24, 2007
    // Modified: Dec 24, 2007
    // This contains function definitions of Real-Time Clock handling
    // routines on DS1302 chip
    
    #include "typedefs.h"
    #include "RTCcustom.h"
    
    static uchar picIO;        // address of MCU pin controlling the IO pin of the RTC
    static uchar picCE;        // address of MCU pin controlling the CE pin of the RTC
    static uchar picSCLK;      // address of MCU pin controlling the SCLK pin of the RTC
    
    void setRTCpin(uchar IOpin, uchar CEpin, uchar SCLKpin)
    {
         picIO = IOpin;
         picCE = CEpin;
         picSCLK = SCLKpin;
    }
    
    uchar MIOreturn(void)
    {
          return picIO;
    }
    
    uchar MCEreturn(void)
    {
          return picCE;
    }
    
    uchar MSCLKreturn(void)
    {
          return picSCLK;
    }
    and RTCcustom.h:
    Code:
    // RTCcustom.h                         <[email protected]>
    // Created: Dec 24, 2007
    // Modified: Dec 24, 2007
    // This contains function declarations of Real-Time Clock handling
    // routines on DS1302
    
    #ifndef RTCCUSTOM_H
    #define RTCCUSTOM_H
    
    void setRTCpin(uchar IOpin, uchar CEpin, uchar SCLKpin);  // set MCU pins controlling RTC
    uchar MIOreturn(void);         // MIO accessor
    uchar MCEreturn(void);         // MCE accessor
    uchar MSCLKreturn(void);       // MSCLK accessor
    
    #endif
    I've tried changing the names of the pin names. I haven't declared the same names in any of the source code and .h files that I know of, yet I'm getting the same complaint.
    With respect to the equivalent of -e parameter supported by gcc, I don't see any support on that particular equivalent.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Try removing "static".
    Try changing "uchar" to "unsigned char".

    gg

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do static module-level variables work anywhere else in your code?

    Embedded compilers can be fickle things at times, with lots of obscure rules to suit the platform in question.

    Though my original comment about a good compiler should never issue "internal compiler error", no matter what you throw at it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I've managed to solve the problem. It appears that mikroC doesn't like how I declared a typedef as:

    Code:
    typedef unsigned char uchar;
    I have one more question. I know in C++ there is a feature such as an accessor function. Is there such function in C? Just wanted to make sure that nothing fishy happens to key variables.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I know in C++ there is a feature such as an accessor function. Is there such function in C? Just wanted to make sure that nothing fishy happens to key variables.
    Accessors make sense in the context of protected/private access. With structs in C there is only (the equivalent of) public access, so one would just access the members of a struct directly. You could still provide functions by which one can access a member of a struct (e.g., if you need to do something whenever a particular member is accessed), but there is no way to enforce access by those functions only.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM