Multiple definitions error compiler is not throwing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saikeerthi
    New Member
    • May 2020
    • 3

    Multiple definitions error compiler is not throwing

    I have same function in multiple files but compiling and linking is happening without error
  • Saikeerthi
    New Member
    • May 2020
    • 3

    #2
    Is there any problem with the compiler I am trying to understand.Than ks in advance

    Comment

    • lewish95
      New Member
      • Mar 2020
      • 33

      #3
      Before actually compiling source the compilation unit is generated from .cpp files. This basically means that all preprocessor directives are computed: all #include will be replaces with content of the included files, all #define'd values will be substituted with corresponding expressions, all #if 0 ... #endif will be removed, etc. So after this step in your case you'll get two pieces of C++ code without any preprocessor directives that will both have definition of same function bool ifFileExist() that is why you get this multiple definition error.

      The fast solution is to mark it as inline bool ifFileExist(). Basically you ask compiler to replace all corresponding function calls with content of the function itself.

      Another approach is to live the declaration of your function in common_utility. h and move definition to common_utility. cpp
      Last edited by Rabbit; May 12 '20, 10:20 PM. Reason: Removed external link

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 655

        #4
        A function is in the global scope by default and all global names must be unique.

        I have same function in multiple files but compiling and linking is happening without error. Is there any problem with the compiler I am trying to understand
        How do you know those multiple files are indeed being linked together and able to produce an ambiguity issue that could lead to a situation when the linker doesn't know which version of the function to call?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          There are lots of possibilities here. Please show us what you have. Post the relevant filenames and function definitions, declarations, and prototypes like this:
          Code:
          [B]header1.h[/B]
          float func(…);
          
          [B]file1.c[/B]
          static int func(…) {…}
          
          [B]file2.c[/B]
          #include "header1.h"
          float func(…) {…}
          
          [B]file3.c[/B]
          double func();
          double func(…) {…}
          The dot-dot-dots are deliberate - no need to give us the argument list or the body of the functions.

          Comment

          • Saikeerthi
            New Member
            • May 2020
            • 3

            #6
            A.c
            *************** ********""
            FUNC(void,a) ABC(void)
            {
            Random function():
            }

            *************** *"*"
            B.c

            FUNC(void,a) ABC(void)
            {
            Random function()
            }
            *************** ***
            ABC is a function which is having the function definition in both
            A.c and B.c files compiler is not throwing multiple definition error .as it expected to throw error.here my doubt is why it is not multiple definition error

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              What is this?
              Code:
              FUNC(void,a)
              Is it a macro?

              Comment

              Working...