Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7958435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:25:21+00:00 2026-06-04T04:25:21+00:00

Case 1: multiple definitions of a function module1.cpp: void f(){} main.cpp: void f(){} //

  • 0

Case 1: multiple definitions of a function

module1.cpp:

void f(){}

main.cpp:

void f(){} // error LNK2005: "void __cdecl f(void)" (?func@@YAXXZ) already defined in module1.obj
int main(){} 

Case 2: multiple definitions of a class

module1.cpp:

class C{};

main.cpp:

class C{}; // OK
int main(){} 

In the Case 1, as expected, (Microsoft) linker comes across two definitions of the same function and issues an error. In Case 2, it allows two definitions of the same class.

Question 1: Why linker doesn’t complain when having multiple definitions of the same class? Is it related to the fact that function name is just the name of the address where its instructions start and class name is the name of a new type?

Furthermore, linker won’t complain even if we use different definitions of the class (I added functions that call class constructors so they appear in the symbol table):

module1.cpp:

class MyClass
{
    int n;
public: 
    MyClass() : n(123){}
};

void func()
{
   MyClass c;
}

main.cpp:

class MyClass
{
   float n;
public: 
   MyClass() : n(3.14f){}
};

int main()
{
   MyClass c;
} 

I set the compiler option so it generates COD file along OBJ files. I can see that both constructors appear under the same mangled name (??0MyClass@@QAE@XZ), each in its own unit (COD file). It is expected that if some symbol is referenced in a module, linker will use its definition from the same module, if it exist. If not, it will be using symbol definition from the module where it is defined. And this can be dangerous as it seems that linker picks symbol from the first object file it comes across:

module1.h:

#ifndef MODULE1_H_
#define MODULE1_H_

void func1();

#endif

module1.cpp:

#include <iostream>
#include "module1.h"

class MyClass
{
    int myValue;

public:
    MyClass() : myValue(123)
    {
        std::cout << "MyClass::MyClass() [module1]" << std::endl;
    }   

    void foo()
    {       
        std::cout << "MyClass::foo() [module1]: n = " << myValue << std::endl;
    }
};

void func1()
{
    MyClass c;
    c.foo();
}

module2.cpp:

#include <iostream>

class MyClass
{   
public:
    MyClass()
    {
        std::cout << "MyClass::MyClass() [module2]" << std::endl;
    }   
};

// it is necessary that module contains at least one function that creates MyClass object
void test2()
{
    MyClass c;
}

main.cpp:

#include "module1.h"

int main()
{
    func1();
}

If object files are listed in this order when passing to the linker:

module2.obj module1.obj main.obj

linker will pick constructor MyClass::MyClass from the first obj file but MyClass::foo from the second one so the output is unexpected (wrong):

MyClass::MyClass() [module2]
MyClass::foo() [module1]: n = 1

If object files are listed in this order when passing to the linker:

module1.obj module2.obj main.obj

linker will pick both MyClass members from the first obj file:

MyClass::MyClass() [module1]
MyClass::foo() [module1]: n = 123

Question 2: Why are linkers designed in the way that they allow multiple class definitions which can lead to errors described above? Isn’t it wrong that linking process depends on the order of object files?

It seems that linker picks the first symbol definition it comes across when scanning object files and then silently discards all subsequent definition duplicates.
Question 3: Is this how linker builds its symbol lookup table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:25:22+00:00Added an answer on June 4, 2026 at 4:25 am

    Regarding your Question 1: Multiple definitions of classes and inline functions are allowed, as long as you don’t violate the One Definition Rule (ODR).

    When you define a function within a class, it is implicitly inline. You invoked undefined behaviour by violating the ODR with the constructors of MyClass.

    The rationale of this behaviour is: When you have an inline function in a class, it is visible in a number of compilation units, with no compilation unit obviously the “preferred” compilation unit. Your toolchain can, however, rely on the ODR and assume that all inlined methods have the same semantics. Therefore, the linker can pick any of the inlined function definitions in linking, since they are all the same.

    The solution to the problem is simple: Don’t violate the ODR.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Figure 1: function templates TemplHeader.h template<typename T> void f(); TemplCpp.cpp template<typename T> void f(){
Case: Developing a standalone java client which will run on different locations on multiple
We have a business case that would be perfect for multiple BackgroundWorkers. As an
I am having a problem using multiple And 's in a Case statement. My
My application uses multiple windows I want to hide one specific window in case
In case of multiple fields with same name iTextSharp acroFields.SetField(Name, Value) sets value for
I have C++ project which consists of multiple (in fact many) .cpp and .h
How does the C++ compiler decide which function/method to call if there are multiple
Hi I'm trying to write multiple case statements to preset the priority of a
Is there a way to fall through multiple case statements without stating case value:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.