C++ call inline function from another cpp file

Jonathan Taws

I'm trying to understand a few basics about extern, static, etc. and tried the following example, but I don't get why I can't call the function "just" because it's (possibly) inlined.

My first file : F1.cpp

 #include <iostream>
 
 void Modify();
 int i;

 int main() {
      i = 1;

      std::cout << "i = " << i << std::endl;
      Modify();
      std::cout << "i = " << i << std::endl;
 
      return 0;
 }

The second file : F2.cpp

#include <iostream>

extern int i;

inline void Modify() {
    i = 99;

    std::cout << "i = " << i << std::endl;

}

With inline keyword in F2.cpp, I get : undefined reference to Modify() in my F1.cpp file. Removing it, the code compiles and works fine.

I assume that the inline keyword in C++ has some sort of behaviour like the static keyword ?

I had a look at this topic aswell, but apart from the fact that the documentation says that an inline function should always be in the header file, I don't get it : C++ inline member function in .cpp file

Thanks for your help !

Vlad from Moscow

According to the C++ Standard (7.1.2 Function specifiers)

4....If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required.

and

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

In C ( section 6.7.4 Function specifiers of the C Standard ) function specifier inline for external functions has different semantic

7....An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ call inline function from another cpp file

From Dev

Call another C++ function on the same .cpp file in android ndk

From Dev

How to call a function from a separate .cpp file?

From Java

Call a function from another file?

From Dev

Access an inline function from another file

From Dev

Calling a function in a .cpp file from a .c file

From Dev

How to call the static function from another c file?

From Dev

C++ How to call a function from another file with the same name as a function in another file when both are included?

From Dev

How do I use a variable from a function of a cpp file on another function in anotehr cpp file?

From Dev

How to call a method defined in main.cpp from another .cpp source file?

From Dev

AngularJS Call function from another file

From Dev

Call function from another file in BrightScript

From Dev

Mediawiki Call hook function from another file

From Dev

Call function from another file in BrightScript

From Dev

Mediawiki Call hook function from another file

From Dev

Refer from 1 cpp file to another C++

From Dev

Call another function from another function C#

From Dev

PHP How to call a function from another class and another file?

From Dev

create java function in a jsp file and call it from another jsp file

From Dev

create java function in a jsp file and call it from another jsp file

From Dev

Qt moving slots function to another cpp file

From Dev

Using the system function to run another .cpp file

From Dev

In kernel module can I call static inline function defined in another header file of the kernel source?

From Dev

Calling a function from another file into another function C++

From Dev

Call function from another function error C++

From Dev

Copying words from one file to another in cpp

From Dev

Accessing method from another cpp file

From Dev

Call another function from within same script file

From Dev

LLVM Insert function call defined from another file

Related Related

  1. 1

    C++ call inline function from another cpp file

  2. 2

    Call another C++ function on the same .cpp file in android ndk

  3. 3

    How to call a function from a separate .cpp file?

  4. 4

    Call a function from another file?

  5. 5

    Access an inline function from another file

  6. 6

    Calling a function in a .cpp file from a .c file

  7. 7

    How to call the static function from another c file?

  8. 8

    C++ How to call a function from another file with the same name as a function in another file when both are included?

  9. 9

    How do I use a variable from a function of a cpp file on another function in anotehr cpp file?

  10. 10

    How to call a method defined in main.cpp from another .cpp source file?

  11. 11

    AngularJS Call function from another file

  12. 12

    Call function from another file in BrightScript

  13. 13

    Mediawiki Call hook function from another file

  14. 14

    Call function from another file in BrightScript

  15. 15

    Mediawiki Call hook function from another file

  16. 16

    Refer from 1 cpp file to another C++

  17. 17

    Call another function from another function C#

  18. 18

    PHP How to call a function from another class and another file?

  19. 19

    create java function in a jsp file and call it from another jsp file

  20. 20

    create java function in a jsp file and call it from another jsp file

  21. 21

    Qt moving slots function to another cpp file

  22. 22

    Using the system function to run another .cpp file

  23. 23

    In kernel module can I call static inline function defined in another header file of the kernel source?

  24. 24

    Calling a function from another file into another function C++

  25. 25

    Call function from another function error C++

  26. 26

    Copying words from one file to another in cpp

  27. 27

    Accessing method from another cpp file

  28. 28

    Call another function from within same script file

  29. 29

    LLVM Insert function call defined from another file

HotTag

Archive