struct question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Morenz

    struct question

    Hi all,
    I've got a problem with a struct, well i want to declare struct in main
    program ( in .c file ) and then use it in a librarie included file( .h
    file ), is it possible?

    Thank in advance
    Morenz
  • pete

    #2
    Re: struct question

    Morenz wrote:[color=blue]
    >
    > Hi all,
    > I've got a problem with a struct,
    > well i want to declare struct in main
    > program ( in .c file ) and then use it in a librarie included file( .h
    > file ), is it possible?[/color]

    Define the structure type in the .h file,
    but do not declare variables (objects) in a .h file.
    #include the .h file in whichever .c files use the type.
    Declare structures of that type (objects), in .c files.

    --
    pete

    Comment

    • Emmanuel Delahaye

      #3
      Re: struct question

      Morenz wrote on 02/08/04 :[color=blue]
      > I've got a problem with a struct, well i want to declare struct in main
      > program ( in .c file ) and then use it in a librarie included file( .h file
      > ), is it possible?[/color]

      Where did you get that a header is "librarie included file" (library,
      actually) ? Give me a name, I'll shoot him for free.

      A header is nothing but an interface file composed of

      - Definitions (macros, constants, types, structures, unions) and
      - Declarations (functions prototypes, object declarations)
      - Eventually, in C99, inlined functions defintions.

      The definition of the functions and objects of the library belong to at
      least one source file (.c) that is used to build the library.

      That said, you have a design problem. A library should not depend on
      user's structure. What do you intent to do exactly ?

      --
      Emmanuel
      The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

      "C is a sharp tool"

      Comment

      • Richard Bos

        #4
        Re: struct question

        pete <pfiland@mindsp ring.com> wrote:
        [color=blue]
        > Morenz wrote:
        >[color=green]
        > > I've got a problem with a struct,
        > > well i want to declare struct in main
        > > program ( in .c file ) and then use it in a librarie included file( .h
        > > file ), is it possible?[/color]
        >
        > Define the structure type in the .h file,
        > but do not declare variables (objects) in a .h file.[/color]

        On the contrary, _declaring_ (extern) objects in a header file is not
        only ok, but often useful. _Defining_ them, however, easily leads to two
        definitions of the same object in your program, which is, of course, not
        good.

        Richard

        Comment

        • pete

          #5
          Re: struct question

          Richard Bos wrote:[color=blue]
          >
          > pete <pfiland@mindsp ring.com> wrote:
          >[color=green]
          > > Morenz wrote:
          > >[color=darkred]
          > > > I've got a problem with a struct,
          > > > well i want to declare struct in main
          > > > program ( in .c file ) and then use it in a librarie included file( .h
          > > > file ), is it possible?[/color]
          > >
          > > Define the structure type in the .h file,
          > > but do not declare variables (objects) in a .h file.[/color]
          >
          > On the contrary, _declaring_ (extern) objects in a header file is not
          > only ok, but often useful. _Defining_ them,
          > however, easily leads to two definitions of the same
          > object in your program, which is, of course, not good.[/color]

          My terminology needs work.

          --
          pete

          Comment

          • pete

            #6
            Re: struct question

            Richard Bos wrote:[color=blue]
            >
            > pete <pfiland@mindsp ring.com> wrote:
            >[color=green]
            > > Morenz wrote:
            > >[color=darkred]
            > > > I've got a problem with a struct,
            > > > well i want to declare struct in main
            > > > program ( in .c file )
            > > > and then use it in a librarie included file( .h file ),
            > > > is it possible?[/color]
            > >
            > > Define the structure type in the .h file,
            > > but do not declare variables (objects) in a .h file.[/color]
            >
            > On the contrary, _declaring_ (extern) objects in a header file is not
            > only ok, but often useful.[/color]

            For matched pairs of .c and .h files:
            I am inclined not to put extern object declarations in a header.
            I think they belong in the .c file.
            If a.c used "extern struct e d", any other .c file linking with a.c,
            wouldn't need to know which external objects were being used in a.c,
            so therefore "extern struct e d",
            should be declared in a.c, instead of in a.h.

            --
            pete

            Comment

            • CBFalconer

              #7
              Re: struct question

              pete wrote:[color=blue]
              >[/color]
              .... snip ...[color=blue]
              >
              > For matched pairs of .c and .h files:
              > I am inclined not to put extern object declarations in a header.
              > I think they belong in the .c file.
              > If a.c used "extern struct e d", any other .c file linking with a.c,
              > wouldn't need to know which external objects were being used in a.c,
              > so therefore "extern struct e d",
              > should be declared in a.c, instead of in a.h.[/color]

              You won't go wrong if you consider the .h file as a means of
              publicizing those portions of the .c file you want to make
              accessible to other compilation units. Thus you declare and
              define the objects in the .c file. If the objects are NOT to be
              accessible externally you mark them static. If they ARE to be
              accessible externally you declare them in the .h file, and use the
              extern keyword for objects other than function prototypes.

              Similarly you put typedefs and macros in the .h file ONLY when you
              need to have them available to other compilation units.

              --
              "I'm a war president. I make decisions here in the Oval Office
              in foreign policy matters with war on my mind." - Bush.
              "Churchill and Bush can both be considered wartime leaders, just
              as Secretariat and Mr Ed were both horses." - James Rhodes.
              "If I knew then what I know today, I would still have invaded
              Iraq. It was the right decision" - G.W. Bush, 2004-08-02


              Comment

              • pete

                #8
                Re: struct question

                CBFalconer wrote:[color=blue]
                >
                > pete wrote:[color=green]
                > >[/color]
                > ... snip ...[color=green]
                > >
                > > For matched pairs of .c and .h files:
                > > I am inclined not to put extern object declarations in a header.[/color][/color]
                [color=blue]
                > You won't go wrong if you consider the .h file as a means of
                > publicizing those portions of the .c file you want to make
                > accessible to other compilation units. Thus you declare and
                > define the objects in the .c file. If the objects are NOT to be
                > accessible externally you mark them static. If they ARE to be
                > accessible externally you declare them in the .h file, and use the
                > extern keyword for objects other than function prototypes.[/color]

                The more I think about what you wrote, the more I like it.

                --
                pete

                Comment

                Working...