Typedef and external linkage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • parag.kanade@gmail.com

    Typedef and external linkage

    Hi,

    I need a refrence of a typedef'ed structure in a file in which it is
    not defined. I cannot include the header file in which the typedef is
    defined.

    file_a.h
    ----------
    typedef struct dummy
    {

    ...
    }struct_a;

    -------------------------------------------------------

    file_b.h
    ----------
    void foo(struct_a var1);
    -------------------------------------------------------

    How do I let the compiler/linker know that struct_a is defined
    somewhere else. I cannot include file_a.h in file_b.h as both the files
    are included in many .c files.

  • pete

    #2
    Re: Typedef and external linkage

    parag.kanade@gm ail.com wrote:
    [color=blue]
    > I cannot include file_a.h in file_b.h as both the files
    > are included in many .c files.[/color]

    The inclusion of both .h files in many .c files,
    shouldn't be a problem.

    /* BEGIN file_a.h */

    #ifndef H_FILE_A
    #define H_FILE_A

    typedef struct dummy
    {

    ...
    }struct_a;

    #endif

    /* END file_a.h */

    --
    pete

    Comment

    • parag.kanade@gmail.com

      #3
      Re: Typedef and external linkage


      pete wrote:[color=blue]
      > parag.kanade@gm ail.com wrote:
      >[color=green]
      > > I cannot include file_a.h in file_b.h as both the files
      > > are included in many .c files.[/color]
      >
      > The inclusion of both .h files in many .c files,
      > shouldn't be a problem.[/color]

      Thanks for the solution. Is there a method which doesn't include
      including file_a.h in file_b.h. Can extern or a forward declaration be
      used?

      -Parag

      [color=blue]
      >
      > /* BEGIN file_a.h */
      >
      > #ifndef H_FILE_A
      > #define H_FILE_A
      >
      > typedef struct dummy
      > {
      >
      > ...
      > }struct_a;
      >
      > #endif
      >
      > /* END file_a.h */
      >
      > --
      > pete[/color]

      Comment

      • Walter Roberson

        #4
        Re: Typedef and external linkage

        In article <1113539291.162 204.243130@g14g 2000cwa.googleg roups.com>,
        <parag.kanade@g mail.com> wrote:
        [color=blue]
        >pete wrote:[color=green]
        >> parag.kanade@gm ail.com wrote:[/color][/color]
        [color=blue][color=green][color=darkred]
        >> > I cannot include file_a.h in file_b.h as both the files[/color][/color][/color]
        [color=blue]
        >Thanks for the solution. Is there a method which doesn't include
        >including file_a.h in file_b.h.[/color]

        Put the definition in file_c.h and #include that in file_a.h
        and file_b.h
        [color=blue]
        >Can extern or a forward declaration be
        >used?[/color]

        No. The compiler needs the structure definition in the
        compilation unit it is in. For example if structure
        member next is a pointer to a character then
        next - 1 would be an unsigned subtraction of 1 byte
        If it is pointer to an integer, then next - 1 would
        be an unsigned subtraction of as many bytes as an integer
        occupies. If it is a void then next - 1 should be a compilation
        error. If next is a double then you have a floating point
        subtraction, if it is an integer then you have a signed
        integer subtraction, and so on. Without the types of the
        fields, it can't know what code to put in.
        --
        Look out, there are llamas!

        Comment

        • parag.kanade@gmail.com

          #5
          Re: Typedef and external linkage

          Got it, but I am not using the struct members in the header, I just
          need the type name, in the implementation file, lets call it file_b.c,
          I am including file_a.h, and the function (foo) needs to be expoerted
          to other modules so I needs its declaration in file_b.h.

          So I was thinking that a forward declaration might help.

          -Parag

          Comment

          • Chris Torek

            #6
            Re: Typedef and external linkage

            In article <1113541040.831 081.277510@g14g 2000cwa.googleg roups.com>
            <parag.kanade@g mail.com> wrote:[color=blue]
            >Got it, but I am not using the struct members in the header, I just
            >need the type name ...[/color]

            Typedef names have no linkage. They have little practical use
            anyway; the only time a typedef is actually required is in some
            relatively obscure corner cases of the va_arg() macro from <stdarg.h>.

            Just stop using the typedefs. Write "struct <structure-name>";
            then the whole problem goes away:

            file1.c:
            struct zorg { ... }; /* declares and defines the structure type */
            /* code that uses the actual structure */

            file2.c:
            struct zorg; /* declares the type without defining it */

            extern struct zorg *new_zorg(void) ;
            extern void act_on_zorg(str uct zorg *);
            extern voide destroy_zorg(st ruct zorg *);

            void intermediate_fu nc(struct zorg *p) {
            ... do some stuff ...
            act_on_zorg(p);
            ... do some more stuff ...
            }

            void top_level_func( void) {
            struct zorg *z;
            ...
            z = new_zorg();
            intermediate_fu nc(z);
            destroy_zorg(z) ;
            }

            Note that file2.c never needs to know what is inside a "struct
            zorg", and no typedefs clutter up the cleanliness of using C's
            type-defining and type-referring keyword "struct", which clearly
            :-) stands for "STRange spelling for User-defined abstraCt Type".

            (For English-speakers, just pretend it *is* the word "type" and
            it will all make sense. Or:

            #define type struct

            type zorg;

            extern type zorg *new_zorg(void) ;

            See? :-) )
            --
            In-Real-Life: Chris Torek, Wind River Systems
            Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
            email: forget about it http://web.torek.net/torek/index.html
            Reading email is like searching for food in the garbage, thanks to spammers.

            Comment

            • parag.kanade@gmail.com

              #7
              Re: Typedef and external linkage

              Not using typedefs is not an option. The system I am currently working
              on has typedefs all over the place and used liberally. I have to live
              with it and find a solution using typedefs.

              -Parag

              Comment

              Working...