2

I have two cpp files :

F1.cpp

using namespace std;

int i;

void Modify();

int main()
{
 i=1;
 cout << "i main 1 = " << i << endl;
 Modify();
 cout << "i main 2 = " << i << endl;

 return 0;
}

F2.cpp

using namespace std;

extern int i;

inline void Modify()
{

  i=99;

  cout << "i modify = " << i << endl;

}

When I launch the executable I get this error : F1.o: In function main : F1.cpp:(.text+0x4a): undefined reference to `Modify()' collect2: error: ld returned 1 exit status

I don't understand why this is happening since the point of an inline function is that the code is copy pasted when the function is called. So when I call Modify() in my main method, I would think that it would paste the code of the Modify() function there, therefore I don't understand why there would be an undefined reference...

Please help!

3 Answers 3

3

The definition of an inline function shall be present in each compilation unit where it is used.

From the C++ 17 Standard (10.1.6 The inline specifier)

2 A function declaration (11.3.5, 12.2.1, 14.3) with an inline specifier declares an inline function. The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions specified in this section shall still be respected.

and

6 An inline function or variable shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case

Sign up to request clarification or add additional context in comments.

7 Comments

Could you please be more explicit? I thought the point of inline functions was to avoid function calling by pasting the function's core everywhere it is called?
Or what happens is that it is first called and then the code is inserted? So here the problem would be that when the function is first called it can't be found?
@CSstudZ It's the other way round actually. Normally function can only be defined in one translation unit (one cpp file). Then linker inserts this definition wherever it is needed. If you would like to have the definition in multiple translation units (e.g. define function in header file), you have to make it inline (and make sure all translation units have the same definition)
@CSstudZ The compiler needs to know the function definition that to be able to decide whether to include it as an inline call or not.
@CSstudZ Either place the function definition in a header and include the header in each compilation unit where the function is used or copy and paste the definition from F2.cpp to F1.cpp.
|
1
void Modify();

This line is actually a function declaration. However, you have not provided the implementation of the function (i.e. function definition). You have defined an inline function in F2.cpp but this is not visible in F1.cpp.

The normal practice is to define inline functions in header files. They are typically very short functions one or two lines in size. What happens is that during compilation the compiler will copy the contents of your inline function in place to wherever it is used. However, declaring a function inline does not guarantee that this will happen. The compiler is free to choose to expand some, all, or none of your inline functions.

You might find this section of C++ FAQ interesting:

Note: It’s imperative that the function’s definition (the part between the {...}) be placed in a header file, unless the function is used only in a single .cpp file. In particular, if you put the inline function’s definition into a .cpp file and you call it from some other .cpp file, you’ll get an “unresolved external” error from the linker.

Comments

0

In your F1.cpp file, Modify() was never actually defined. That's also what the error being returned is referring to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.