Results 1 to 7 of 7

Thread: Multiple definition of a function (MinGW)

  1. #1
    Join Date
    May 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Multiple definition of a function (MinGW)

    Can anyone explain me why MinGW compiler gives multiple definition error about functions implemented in the two separate cpps and not declared in the headers?

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Multiple definition of a function (MinGW)

    If the functions wind up in the same linkage, there's going to be a conflict. You must differentiate functions by name, argument list, namespace or some other scoping mechanism if they will be combined into a single executable or library.

  3. The following user says thank you to SixDegrees for this useful post:

    spirt (12th June 2011)

  4. #3
    Join Date
    May 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Multiple definition of a function (MinGW)

    I thought that cpp already provides a separate scope, if function declared and implemented only there and not exported, there shouldn't be any problem

  5. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Multiple definition of a function (MinGW)

    Quote Originally Posted by spirt View Post
    I thought that cpp already provides a separate scope, if function declared and implemented only there and not exported, there shouldn't be any problem
    How could it possibly do that? If two definitions of the same function, with identical parameters, collide in the same linkage unit, there is no way for the linker to determine which one should be used. The preprocessor has nothing to do with this problem.

  6. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple definition of a function (MinGW)

    Quote Originally Posted by spirt View Post
    Can anyone explain me why MinGW compiler gives multiple definition error about functions implemented in the two separate cpps and not declared in the headers?
    Well, you should be using a different class for the two seperate cpp files so this shouldn't be problem.

    If you are using global functions then obviously defining the same function in two different cpp files will cause confusion at link time which to use, so either make one of them file local or put them in a class. Same goes for global variables. The linker is not going to check to see if you have included such functions in a header file.

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Multiple definition of a function (MinGW)

    Quote Originally Posted by sprit
    Can anyone explain me why MinGW compiler gives multiple definition error about functions implemented in the two separate cpps and not declared in the headers?
    Even though functions are implemented in two separate source files, if they belong to same namespace, the linker would be confused which one to use for linking and ends up in throwing error.

    Quote Originally Posted by sprit
    I thought that cpp already provides a separate scope, if function declared and implemented only there and not exported, there shouldn't be any problem
    Yes both the functions have different scope (as not declared in include files), but scopes are different for compiler & linker. For compiler, scope is single translated unit (including any extern symbols). Linker's job is to connect all the translation units, to so form the final executable, so for linker the scope includes all translations units, and hence the error.

    C++ provides various ways to deal with situation, like namespace separation, object encapsulation using classes / structs, function overloading etc, you can use one of these methods.

  8. #7
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple definition of a function (MinGW)

    I'd suggest to use anonymous namespaces to avoid such conflicts:

    Qt Code:
    1. //a.cpp
    2. namespace
    3. {
    4. void function() {...}
    5. }
    6.  
    7. //b.cpp
    8. namespace
    9. {
    10. void function() {...}
    11. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Non-existing multiple definition bug
    By uGin in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2011, 16:53
  2. Replies: 1
    Last Post: 17th May 2011, 16:12
  3. qmake - multiple definition ?
    By zaphod77 in forum Installation and Deployment
    Replies: 3
    Last Post: 6th April 2011, 12:11
  4. Multiple definition of a variable?
    By T0bi4s in forum Newbie
    Replies: 5
    Last Post: 14th January 2010, 22:13
  5. Getting multiple definition error from qatomic_x86_64.h
    By markcole in forum General Programming
    Replies: 1
    Last Post: 30th August 2007, 19:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.