• AVR Freaks

avoiding "redefinition of typedef"

Author
sjb741
Super Member
  • Total Posts : 834
  • Reward points : 0
  • Joined: 2010/01/25 08:45:39
  • Location: 0
  • Status: offline
2010/05/18 02:58:17 (permalink)
0

avoiding "redefinition of typedef"

Here is a problem I get a lot:
My code :
  

#include "GenericTypeDefs.h"    //Part of the Microchip's "MCHPFSUSB" framework

3rd party library
   

typedef unsigned char BYTE;

result
   error: redefinition of typedef 'BYTE'

"GenericTypeDefs.h" holds lots of useful definitions, the 3rd-party header holds some definitions which may be particular to that library, and some definitions which, unfortunately, are a subset of "GenericTypeDefs.h".

In one 3rd-party library ("FAT16/32 File IO Library"), they had this (apparantly an attempt to avoid this situation):

#ifndef BYTE
    typedef unsigned char BYTE;
#endif


I tested this technique, and it seems to not do what the author intended; here is my test:


typedef unsigned char TEST_BYTE;
#ifdef TEST_BYTE
    #warning "TEST_BYTE-Already defined"   
#else
    #warning "TEST_BYTE-NOT defined yet"
    typedef unsigned char TEST_BYTE;    //error: redefinition of typedef 'TEST_BYTE'
#endif

Here is a particular example of how the conflict arises in practice:
My Code

#include "GenericTypeDefs.h"
#include "fat_filelib.h


Their code

fat_filelib.h:
   #include "fat_access.h"
fat_access.h:
   #include "fat_defs.h"
fat_defs.h:
   #include "fat_types.h"

fat_types.h:
...
   #ifndef BYTE
      typedef unsigned char BYTE;
   #endif
...



#1

15 Replies Related Threads

    BitWise
    Super Member
    • Total Posts : 1238
    • Reward points : 0
    • Joined: 2004/11/09 13:24:20
    • Location: UK
    • Status: offline
    RE: avoiding "redefinition of typedef" 2010/05/18 03:31:53 (permalink)
    0
    #ifdef tests if a macro replacement symbol (e.g. one created by a #define) exists within the pre-processor. It can't be used to test if a typedef has been defined.
     
    You would be better off creating a set of symbols with different names from the target typedefs which can be used to turn individual typedefs on or off

    #ifndef BYTE_DEFINED
    typedef unsigned char BYTE
    # define BYTE_DEFINED
    #endif


    Throughout your life advance daily, becoming more skillful than yesterday, more skillful than today. This is never-ending.

    Yamamoto Tsunetomo (1659-1719)
    #2
    tony4l
    Super Member
    • Total Posts : 473
    • Reward points : 0
    • Joined: 2003/11/07 12:36:16
    • Status: offline
    RE: avoiding "redefinition of typedef" 2010/05/18 03:33:10 (permalink)
    0
    well, you can't test a typedef with an #ifdef (although the naming might suggest otherwise). A typedef is processed by the compiler and that only hits if the preprocessor has already processed your file.

    So, in your 'technique test', try #defining the type instead of using typedef. It's rather ugly but it should work around the problem you are seeing.

    That is:
    #define TEST_BYTE (unsigned char)
    that way, you can use #ifdef to check if TEST_BYTE has been defined and if it has not, it announce it to the compiler.
    #ifdef TEST_BYTE
    #warning foobar
    #else
    typedef unsigned char TEST_BYTE;
    #endif


    Again, though the resulting code is probably the same, it's kind of smelly. Yet another reason to have C++ support for PICs :)

    beware of ASCII characters when posting. better safe than sorry!
    #3
    Brem
    Junior Member
    • Total Posts : 120
    • Reward points : 0
    • Joined: 2005/09/22 12:30:34
    • Location: Amsterdam
    • Status: offline
    RE: avoiding "redefinition of typedef" 2010/05/18 03:43:03 (permalink)
    0
    Or if you don't want to change the 3rd party files, you could add a:

    #ifdef
    __GENERIC_TYPE_DEFS_H_
    #define BYTE BYTE
    #endif

     
    #4
    sjb741
    Super Member
    • Total Posts : 834
    • Reward points : 0
    • Joined: 2010/01/25 08:45:39
    • Location: 0
    • Status: offline
    RE: avoiding "redefinition of typedef" 2010/05/18 05:30:50 (permalink)
    0
    It seems the 3rd-party test (for previous definitions) is not valid (as I suspected).

    Brem: That is a *fab* suggestion - it works, thanks wink
    #5
    aschen0866
    Super Member
    • Total Posts : 4573
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    RE: avoiding "redefinition of typedef" 2010/05/18 07:16:01 (permalink)
    0
    Just curious - I tried

    #include <GenericTypeDefs.h>

    typedef unsigned char BYTE;
    typedef unsigned char BYTE;
    typedef unsigned char BYTE;
    typedef unsigned char BYTE;

    BYTE test_byte = 123;

    int main(void)
    {


    while(1);

    return (0);
    }

    and I am getting no error at all (C30 v3.21 with -Wall option). How did you get "redefinition of typedef 'BYTE'" to come out?
    #6
    sjb741
    Super Member
    • Total Posts : 834
    • Reward points : 0
    • Joined: 2010/01/25 08:45:39
    • Location: 0
    • Status: offline
    RE: avoiding "redefinition of typedef" 2010/05/24 06:13:07 (permalink)
    0
    That's an interesting point...
    My code has this:

    #include "GenericTypeDefs.h" //Part of the Microchip's "MCHPFSUSB" framework
    In the original code, I had added the path of the MCHPFSUSB framework to the project (typically C:\Microchip Solutions\Microchip\Include), and I (wrongly, it seems!) assumed that was the only place this header existed.

    I tried your example in a new project and got no errors; I then added the path and the errors appeared.

    A quick look in the "\MPLAB C30\docs" PDF files yields no mention of this header.
    post edited by sjb741 - 2010/05/24 06:17:03
    #7
    RMiranda
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2012/07/08 16:11:26
    • Location: 0
    • Status: offline
    RE: avoiding "redefinition of typedef" 2012/07/08 16:15:09 (permalink)
    0
    Hi, where do you add this?
    Brem 
     Or if you don't want to change the 3rd party files, you could add a:

    #ifdef
    __GENERIC_TYPE_DEFS_H_
    #define BYTE BYTE
    #endif
     

    I tried it before the redefinition of BYTE, but i get the same error
    #8
    sjb741
    Super Member
    • Total Posts : 834
    • Reward points : 0
    • Joined: 2010/01/25 08:45:39
    • Location: 0
    • Status: offline
    RE: avoiding "redefinition of typedef" 2012/07/11 01:46:34 (permalink)
    0
    Hi,
    I just looked at my old code and it looks like I did the same as you:
    #if defined(__18CXX) || defined(__C30__)
        #include "Compiler.h"            //Part of "MCHPFSUSB" framework
        #include "GenericTypeDefs.h"     //Part of "MCHPFSUSB" framework   
        #include "HardwareProfile.h"
    #endif//#if defined(__18CXX) || defined(__C30__)

    //Avoid conflicts in "fat_types.h"
    #ifdef __GENERIC_TYPE_DEFS_H_
        #define BYTE BYTE   //One of files below can now typedef BYTE (again)
        #define UINT16 UINT16
        #define UINT32 UINT32
    #endif
     
    #include "test_fat.h"  //This (or some include) typedefs BYTE

    ...
     
     
    #9
    RMiranda
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2012/07/08 16:11:26
    • Location: 0
    • Status: offline
    RE: avoiding "redefinition of typedef" 2012/07/11 04:35:36 (permalink)
    0
    Hi, i tried: 
    #define BYTE BYTE
    with and without: 
    #ifdef __GENERIC_TYPE_DEFS_H_ //... #endif 
     but still got the same redefinition error on my application specific:
    typedef unsigned char BYTE

    But i found a different way around it, instead of typedef i now use #define, in my App BYTE:
    #define BYTE unsigned char//typedef unsigned char BYTE

    it solved the error, and a lot of warnings due to PIC32.BYTE != MyApp.BYTE, but i'm still a little in doubt if my workaround it's really "safe".
     
    #10
    aschen0866
    Super Member
    • Total Posts : 4573
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    RE: avoiding "redefinition of typedef" 2012/07/11 07:28:09 (permalink)
    0
    RMiranda
    ...
    But i found a different way around it, instead of typedef i now use #define, in my App BYTE:
    #define BYTE unsigned char//typedef unsigned char BYTE

    it solved the error, and a lot of warnings due to PIC32.BYTE != MyApp.BYTE, but i'm still a little in doubt if my workaround it's really "safe".

    I wonder - If you can indeed change your own code from the existing

    typedef unsigned char BYTE;

    to

    #define BYTE unsigned char

    then why not just get rid of your typedef altogether? Since BYTE is already defined in GenericTypeDefs.h, is there any reason you can't use GenericTypeDefs.h?
    #11
    RMiranda
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2012/07/08 16:11:26
    • Location: 0
    • Status: offline
    RE: avoiding "redefinition of typedef" 2012/07/11 14:51:46 (permalink)
    0
    lol now that i looked at GenricTypeDefs.h, it's the same definition, duh....but my really "beef" it's with WORD:
       
    // GenricTypeDefs.h
    typedef unsigned short int WORD;
    --------------------------------------------
    //myAppTypes.h
    typedef signed int WORD //or with my workaround: #define WORD signed int

     
    It compiles with no errors, but i'm uncertain as to this is really a workaround...the app it self is not working, but it's hard to see right away if this definition is the problem, since i'm migrating this app from a UNIX environment, don't have a debugger, and can't seem to put debug strings on the HyperTerminal, so debugging consists in mLED...
    post edited by RMiranda - 2012/07/11 14:58:41
    #12
    sjb741
    Super Member
    • Total Posts : 834
    • Reward points : 0
    • Joined: 2010/01/25 08:45:39
    • Location: 0
    • Status: offline
    RE: avoiding "redefinition of typedef" 2012/07/12 04:32:17 (permalink)
    0
    If you simply want to override the "GenricTypeDefs.h" definition then you seem to have already hit on a solution, test:
    --------
     TRISA=0;
     PORTA = 0;
     typedef long int ATYPE;
     ATYPE T1;
     char i = sizeof(T1);
     PORTA = i;//4
     #define ATYPE char
     ATYPE T2;
     i = sizeof(T2);
     PORTA = i;//1
    -------
    P.S.:
    I was lucky. The 3rd party code (containing the re-definition) already had this

       #ifndef BYTE  //<---Necessary otherwise "#define BYTE BYTE" won't help
           typedef signed char BYTE;
       #endif

    it also had the same definitions as "GenricTypeDefs.h", so I did not need/want the 3rd party code to alter the meanings of the 'typedef' names.
    #13
    RMiranda
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2012/07/08 16:11:26
    • Location: 0
    • Status: offline
    RE: avoiding "redefinition of typedef" 2012/07/12 05:46:57 (permalink)
    0
    i didn't quite get your example, but
    you seem to have already hit on a solution

    with my workraund in the 3rd party header?
    #define BYTE unsigned char

     
    #14
    aschen0866
    Super Member
    • Total Posts : 4573
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    RE: avoiding "redefinition of typedef" 2012/07/12 07:35:25 (permalink)
    0
    RMiranda

    ....but my really "beef" it's with WORD:
        
    // GenricTypeDefs.h
    typedef unsigned short int WORD;
    --------------------------------------------
    //myAppTypes.h
    typedef signed int WORD //or with my workaround: #define WORD signed int


    It compiles with no errors, but i'm uncertain as to this is really a workaround...

    I would avoid doing this because WORD is typically treated as an unsigned 16-bit type and it needs to remain unsigned to work with Microchip's Peripheral Libraries and any other software stacks, e.g., TCP/IP, USB, MDD File System, etc..
     
    #15
    RMiranda
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2012/07/08 16:11:26
    • Location: 0
    • Status: offline
    RE: avoiding "redefinition of typedef" 2012/07/12 09:15:05 (permalink)
    0
    humm so that must be why i cant seem to put stuff in usb uart...guess i'll have to replace the WORD in the 3rd party source/headers with something different...it's gonna be a long night...thanks for the help!
    #16
    Jump to:
    © 2020 APG vNext Commercial Version 4.5