duplicate functions in multiple modules

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

    #1

    duplicate functions in multiple modules

    Hi,

    I have a project with multiple c files. Some of these modules have
    functions that have the same name and their operations are exactly the
    same.

    My rusty memory tells me there is a way with preprocessor directives to
    make sure these functions are not included more than once, but I forgot
    how to.

    Can somebody please give me an example?

  • Fred Kleinschmidt

    #2
    Re: duplicate functions in multiple modules


    "wanwan" <ericwan78@yaho o.comwrote in message
    news:1163460646 .880065.46480@m 7g2000cwm.googl egroups.com...
    Hi,
    >
    I have a project with multiple c files. Some of these modules have
    functions that have the same name and their operations are exactly the
    same.
    >
    My rusty memory tells me there is a way with preprocessor directives to
    make sure these functions are not included more than once, but I forgot
    how to.
    >
    Can somebody please give me an example?
    >
    Sounds like you are thinking of the
    #ifndef xxx
    #define xxx
    ...
    #endif
    that is used to ensure that INCLUDE files are not included multiple times.
    However, for .c files, you need to do other things to ensure there
    are no name clashes.

    Some ways:
    For each function that is not a public function called from outside the
    file, make the function static.

    Add a prefix on each function that is publicly available; say,
    a two-letter prefix that indicates the file or file grouping.

    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project


    Comment

    • sofiandbrice

      #3
      Re: duplicate functions in multiple modules

      The only answer I can think of (given the low amount of details you are
      giving) is to surround these functions (declaration and implementation)
      with preprocessor directives as follow:

      #ifndef XXX //if not defined yet
      #define XXX //now it is defined
      your functions here
      ....
      #endif

      You should normally always surround header files with such declaration,
      to make sure they are not included several times.
      Remember that C doesn't support function overloading, so it's generally
      not a good idea to have functions with the same name. But you may have
      your reasons.



      Brice




      wanwan wrote:
      Hi,
      >
      I have a project with multiple c files. Some of these modules have
      functions that have the same name and their operations are exactly the
      same.
      >
      My rusty memory tells me there is a way with preprocessor directives to
      make sure these functions are not included more than once, but I forgot
      how to.
      >
      Can somebody please give me an example?
      >

      Comment

      • Jack Klein

        #4
        Re: duplicate functions in multiple modules

        On 13 Nov 2006 15:30:47 -0800, "wanwan" <ericwan78@yaho o.comwrote in
        comp.lang.c:
        Hi,
        >
        I have a project with multiple c files. Some of these modules have
        functions that have the same name and their operations are exactly the
        same.
        Your description is not clear. C does not have "modules". C has
        "translatio n units". In general terms, a translation unit consists of
        what we usually call a source file and any headers and anything else
        it includes.
        My rusty memory tells me there is a way with preprocessor directives to
        make sure these functions are not included more than once, but I forgot
        how to.
        There is no preprocessor directive that can help you if you have more
        than one source file that defines a function with external linkage and
        the same name. Other than the nasty macros in each file (or all but
        one) redefining the name. But it is better to rename them yourself.
        Can somebody please give me an example?
        If you have defined functions or objects in header files without the
        static keyword and included such header files in more than one source
        file, there is nothing the preprocessor can do, either.

        You need to post a cut-down sample, such as a header file and two
        source files that contain such functions. But the best option is not
        to do that.

        A function should be defined in exactly one source file. A prototype
        for that function should be placed in a header file. That header file
        should be included in the source file that defines the function, and
        also in any other source files that want to call the function.

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://c-faq.com/
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++

        Comment

        • sam_cit@yahoo.co.in

          #5
          Re: duplicate functions in multiple modules

          Remember that C doesn't support function overloading, so it's generally
          Even C++ doesn't support function overloading if this is used in the
          c++ files,

          #ifdef __cplusplus
          extern "C" {
          #endif

          ;-)

          Comment

          Working...