Hot!Underscores in ifndef

Author
swmcl
Senior Member
  • Total Posts : 125
  • Reward points : 0
  • Joined: 2014/05/10 13:54:42
  • Location: Queensland
  • Status: offline
2017/07/16 20:17:06 (permalink)
0

Underscores in ifndef

In the code generated by MCC say in fvr.h I see,

#ifndef _FVR_H
#define _FVR_H

In interrupt_manager.h I see,

#ifndef INTERRUPT_MANAGER_H
#define INTERRUPT_MANAGER_H

What is going on with the leading underscores?  Some files have 'em, some do not.  So far, interrupt_manager.h, pin_manager.h and mcc.h do not have them while all the rest of the files do have the leading underscore.
#1

8 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 1238
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: Underscores in ifndef 2017/07/16 20:41:57 (permalink)
    3 (1)
    Now what?
    #2
    jtemples
    Super Member
    • Total Posts : 10192
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Underscores in ifndef 2017/07/16 20:50:50 (permalink)
    3 (1)
    I'm guessing different people wrote the code and there isn't a coding standard, or it wasn't followed.
    #3
    CinziaG
    rome burns :D
    • Total Posts : 3135
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: Underscores in ifndef 2017/07/17 02:04:57 (permalink)
    0
    Yeah,
    though users are usually discouraged from using a leading underscore, in "include guards" they were always there in Microsoft C include files. Just a habit, so to say Smile
    #4
    du00000001
    Just Some Member
    • Total Posts : 1238
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: Underscores in ifndef 2017/07/17 02:11:08 (permalink)
    3 (1)
    For include guards I also encountered a style with a leading plus a trailing underscore (==> _FVR_H_).
    The intention is not to collide with "usual"  #defines (perfect if you're usually not using leading underscores).
    #5
    Mysil
    Super Member
    • Total Posts : 2386
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Underscores in ifndef 2017/07/17 06:25:40 (permalink)
    4 (2)
    Hi,
    The leading underscore is a way to try to avoid naming conflicts,
    that is to avoid problems by the same symbol beeing used for different purposes.
     
    The convention is that 'system libraries' use a leading underscore for macro symbols,
    so application code and application libraries code should use macro symbols that do not start with underscore.
     
    Whether code generated by MCC should be regarded as 'system' code, or 'application' code,
    may be open to debate, but code generated MCC is tested together with the compiler that is expected to be used, so any conflict between symbols used by the compiler, and symbols used by MCC should have been solved already.
     
    Device support header files, use symbols with a leading underscore for macro symbols for register bits and fields,
    but SFR register names have no underscore, so SFR register names make a lot of reserved names without a leading underscore. These are mostly all uppercase names, except a few registers in PIC32 MIPS system coprocessor, CP0, but those registers cannot be accessed directly by C code anyway.
     
       Mysil
     
     
    #6
    swmcl
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Underscores in ifndef 2017/07/17 14:15:10 (permalink)
    3 (1)
    Thank you Mysil.
     
    I guess from my newbie viewpoint I'll just try to make it consistent across all my files. It looks as though a best practice might be to not use the underscores at either end. 
    #7
    mlp
    boots too small
    • Total Posts : 496
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: Microchip XC8 and XCLM team
    • Status: offline
    Re: Underscores in ifndef 2017/07/18 07:28:54 (permalink)
    4.67 (3)
    swmcl
    What is going on with the leading underscores?  Some files have 'em, some do not.



    Identifiers beginning with {underscore}{underscore} or {underscore}{uppercase} are reserved for use by the Implementer. The User is guaranteed that all non-reserved identifiers are available for use in their own code.
    We are the Implementer, you are the User.
     
    The tricky bit arises in source code that we supply that is perhaps intended to be modified by you.
     
    In My Humble (hah!) Opinion,
    • header file skeletons generated by MPLAB X when you tell it to create a New Header file in your project, intended to be filled out by you, are User code and so their include-guards should not have leading underscores;
    • "drivers" generated by MCC etc., intended to be used as-is, are Implementor code and their include-guards should have leading underscores;
    and anything not matching the above is a bug and you should log a support ticket to get it fixed.

    Please, before you post, read the Forum Guidelines
    To get a useful answer, always state which PIC you are using!
     
    Mark (not paid to state the opinions of Microchip Technology Inc.)


    #8
    swmcl
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Underscores in ifndef 2017/07/18 17:21:44 (permalink)
    0
    Just a note to say that when creating a new .h file using "File/New File" it does not automatically use underscores.  So in this way it works as has been generally recommended by mlp, mysil and others in this thread.
    #9
    Jump to:
    © 2017 APG vNext Commercial Version 4.5