-3

I would like to ask a question about the output when I called an inline function, that was declared inside MyHeader.h, which was defined in both source_1.cpp and source_2.cpp files, using the int main() function inside source_2.cppfile.

When I do such a call, the one defined inside the source_1.cpp function is called. The codes are like;

My MyHeader.h contains the following line of codes:

#pragma once
#include <iostream>

inline int func(void);

My source_1.cpp contains the following line of codes:

#include "MyHeader.h"

int func(void)
{
    std::cout << "func(), inside source_1, was called. 102 * 102 will be returned." << "\n";
    return 102 * 102;
}

void source_1(void)
{
    std::cout << "source_1() was called. func() will be called." << "\n";
    func();
}

My source_2.cpp contains the following line of codes:

#include "MyHeader.h"

int func(void)
{
    std::cout << "func(), inside source_2, was called. 102 * 102 will be returned." << "\n";
    return 102 * 102;
}

void source_2(void)
{
    std::cout << "source_2() was called. func() will be called." << "\n";
    func();
}


int main()
{
    std::cout << "main() was called." << "\n";
    source_2();
}

This is the output:

main() was called.
source_2() was called. func() will be called.
func(), inside source_1, was called. 102*102 will be returned.

I wish to know the reason.

Rudi
  • 19,366
  • 3
  • 55
  • 77
  • 2
    Can you please tell me what's in that picture? Unfortunately I am blind, and my screen reader spells out only gibberish there. – πάντα ῥεῖ Oct 04 '20 at 10:54
  • Here is the text in the picture, main() was called. source_2() was called. func() will be called. func(), inside source_1, was called. 102*102 will be returned. – Ahmet Kandemir PEHLİVANLI Oct 04 '20 at 11:06
  • Can't you put that text as code formatted in your question please? – πάντα ῥεῖ Oct 04 '20 at 11:14
  • `main() was called. source_2() was called. func() will be called. func(), inside source_1, was called. 102*102 will be returned.` Is it good now? – Ahmet Kandemir PEHLİVANLI Oct 04 '20 at 11:18
  • 1
    It would be very helpful if you remove the image from the question and add the output with code formatting in the question. – Akib Azmain Turja Oct 04 '20 at 15:52
  • I answered your question. If my answer answers your question, please accept by clicking the tick icon next to my answer. – Akib Azmain Turja Oct 04 '20 at 17:38
  • I'm glad you found a solution to your problem. However, an actual answer/solution should **not** be edited into your question. In general, you should [edit] the question to *clarify* it, but not to include an answer within it. You should create your own answer with the code/solution you used to solve your problem, and then accept it (the system may require a 48 hour delay prior to doing so). When you've solved the problem yourself, [answering your own question is encouraged](/help/self-answer). – double-beep Oct 05 '20 at 04:54

2 Answers2

1

You violated the one definition rule. You are not permitted to have non-static objects in your program with different implementations. The compiler/linker is free to choose any implementation it finds, which can lead to very hard to find bugs.

Rudi
  • 19,366
  • 3
  • 55
  • 77
0

Your source_2 function called func function of source_1.cpp because func is declared as inline , not static . When the linker gets a inline function, it remove all copies of that function except one. So the func function of source_1.cpp is kept, other one is removed by the linker. If you want to have two separate implementation of one function in two source file, declare the function as static . This prevents the linker from removing multiple functions of same name.

Akib Azmain Turja
  • 1,142
  • 7
  • 27