variable declaration within a .h file

This is a discussion on variable declaration within a .h file within the C Programming forums, part of the General Programming Boards category; I'm not sure what's wrong with declaring variables only visible to RTCcustom.h and RTCcustom.c files as shown in the highlighted ...

  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                         <stanigator@gmail.com>
    // 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
    32,854
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,854
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,854
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,854
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I have changed my RTCcustom.c file as shown:
    Code:
    // RTCcustom.c                         <stanigator@gmail.com>
    // 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                         <stanigator@gmail.com>
    // 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,702
    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
    32,854
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    22,306
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:27 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21