Header file with function implementation problem

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

    Header file with function implementation problem

    Hello All

    Received a header file from a supplier that defines an interface to
    implement but it's giving me a problem, I reproduce the general
    structure of the header file below;

    #ifndef XYZ_H

    various #define(s)

    #ifndef _ABC

    void someFunc();

    void someOtherFunc() ;

    void implementedFunc () {
    xyz;
    }

    #endif

    #endif

    The problem is that all my files that include this header xyz.h
    complains about multiple definition of symbol implemetedFunc( ).

    We are running HP-UX on Itanium.

    Any advice would be appreciated.

    Thank you,

    B
  • Keith Thompson

    #2
    Re: Header file with function implementation problem

    bcpkh <vanheerden.bra am@gmail.comwri tes:
    Hello All
    >
    Received a header file from a supplier that defines an interface to
    implement but it's giving me a problem, I reproduce the general
    structure of the header file below;
    >
    #ifndef XYZ_H
    >
    various #define(s)
    >
    #ifndef _ABC
    >
    void someFunc();
    >
    void someOtherFunc() ;
    >
    void implementedFunc () {
    xyz;
    }
    >
    #endif
    >
    #endif
    >
    The problem is that all my files that include this header xyz.h
    complains about multiple definition of symbol implemetedFunc( ).
    >
    We are running HP-UX on Itanium.
    >
    Any advice would be appreciated.
    Function definitions don't belong in headers.

    You said that the header "defines in interface to implement". Perhaps
    the implementation of implementedFunc is just shown as an example. If
    you're expected to modify the header for your own use anyway, it's not
    much of a problem. If you're expected to use it as is, complain to
    the supplier.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • viza

      #3
      Re: Header file with function implementation problem

      On Jun 10, 5:47 pm, Keith Thompson <ks...@mib.orgw rote:
      bcpkh <vanheerden.br. ..@gmail.comwri tes:
      The problem is that all my files that include this header xyz.h
      complains about multiple definition of symbol implemetedFunc( ).
      Function definitions don't belong in headers.
      What about static / inline / static inline functions?

      Comment

      • Stephen Sprunk

        #4
        Re: Header file with function implementation problem

        bcpkh wrote:
        Received a header file from a supplier that defines an interface to
        implement but it's giving me a problem, I reproduce the general
        structure of the header file below;
        >
        #ifndef XYZ_H
        >
        various #define(s)
        Did you remember to #define XYZ_H here? Without that, your header guard
        does nothing.
        #ifndef _ABC
        >
        void someFunc();
        >
        void someOtherFunc() ;
        >
        void implementedFunc () {
        xyz;
        }
        >
        #endif
        >
        #endif
        >
        The problem is that all my files that include this header xyz.h
        complains about multiple definition of symbol implemetedFunc( ).
        Unless the function is static (and preferably inline too), the
        definition should not be in a header, only a declaration. All normal
        function definitions go in a .c file.

        S

        Comment

        • Barry Schwarz

          #5
          Re: Header file with function implementation problem

          On Tue, 10 Jun 2008 09:36:20 -0700 (PDT), bcpkh
          <vanheerden.bra am@gmail.comwro te:
          >Hello All
          >
          >Received a header file from a supplier that defines an interface to
          >implement but it's giving me a problem, I reproduce the general
          >structure of the header file below;
          >
          >#ifndef XYZ_H
          >
          >various #define(s)
          Can we assume one of these is for XYZ_H?
          >
          >#ifndef _ABC
          >
          >void someFunc();
          You do realize that none of these are valid prototypes. Hopefully
          *you* left out the parameter specifications for the sake of brevity
          and they are included in the header.
          >
          >void someOtherFunc() ;
          >
          >void implementedFunc () {
          xyz;
          >}
          This is not a declaration but a definition. And still not a
          prototype.
          >
          >#endif
          >
          >#endif
          >
          >The problem is that all my files that include this header xyz.h
          >complains about multiple definition of symbol implemetedFunc( ).
          >
          The include guard (XYZ_H) only protects you from multiple inclusion in
          the same translation unit (source file) and then only if there is a
          #define directive for it in some of the code you omitted.

          The multiple definition guard (_ABC) requires you to decide in which
          source file (singular) you want the definition of implementedFunc to
          appear. In all other source files, you need to include the
          preprocessing directive
          #define _ABC
          so that the compiler will know to skip over the definition of
          implementedFunc . Without this directive, implementedFunc will be
          compiled with each source file that includes xyz.h and the linker will
          correctly report that it is defined multiple times.

          By the way, this approach sucks. At the very least the test should be
          reversed so you only have to specify _ABC once instead on n-1 times.
          But functions and objects should never be defined in a header file
          anyway, only declared. The supplier should provide you either
          1 - the object file for implementedFunc in a format suitable for
          your linker, or
          2 - the source file for implementedFunc separate from xyz.h so
          you can compile it yourself.


          Remove del for email

          Comment

          • EventHelix.com

            #6
            Re: Header file with function implementation problem

            On Jun 10, 12:36 pm, bcpkh <vanheerden.br. ..@gmail.comwro te:
            Hello All
            >
            Received a header file from a supplier that defines an interface to
            implement but it's giving me a problem, I reproduce the general
            structure of the header file below;
            >
            #ifndef XYZ_H
            >
            various #define(s)
            >
            #ifndef _ABC
            >
            void someFunc();
            >
            void someOtherFunc() ;
            >
            void implementedFunc () {
            xyz;
            >
            }
            >
            #endif
            >
            #endif
            >
            The problem is that all my files that include this header xyz.h
            complains about multiple definition of symbol implemetedFunc( ).
            >
            We are running HP-UX on Itanium.
            >
            Any advice would be appreciated.
            >
            Thank you,
            >
            B
            The header file multiple inclusion is not implemented correctly.

            The following article should help:



            --
            Sequence diagram based systems engineering and architecture design tool. Built in support for alternative scenarios and multi-tier architectures.

            Sequence diagram based systems engineering tool

            Comment

            • Ian Collins

              #7
              Re: Header file with function implementation problem

              viza wrote:
              On Jun 10, 5:47 pm, Keith Thompson <ks...@mib.orgw rote:
              >bcpkh <vanheerden.br. ..@gmail.comwri tes:
              >
              >>The problem is that all my files that include this header xyz.h
              >>complains about multiple definition of symbol implemetedFunc( ).
              >
              >Function definitions don't belong in headers.
              >
              What about static / inline / static inline functions?
              They are OK, your example was neither.

              --
              Ian Collins.

              Comment

              • Richard Heathfield

                #8
                Re: Header file with function implementation problem

                EventHelix.com said:

                <snip>
                The header file multiple inclusion is not implemented correctly.
                >
                The following article should help:
                >
                http://www.eventhelix.com/realtimema...dePatterns.htm
                Unlikely, since it seems to be about C++ rather than C.

                It is generally a bad idea to assume that C++ rules are the same as C rules
                for features that they have in common. It does sometimes turn out to be
                the case, but it is still a poor assumption. If you have an article about
                the C preprocessor rather than the C++ preprocessor, bring it on.

                I note that the first code example on your page, if taken as C, invades
                implementation namespace (whether that rule is the same in C++, I don't
                know), and then rounds off with a multitude of syntax errors.

                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                Working...