Inline Function are those function whose definitions are small and be substituted at the place where its function call is happened. Function substitution is totally compiler choice.
Let’s take below example:
In function `main': undefined reference to `foo'
Why this error happened?
This is one of the side effect of GCC the way it handle inline function. When compiled, GCC performs inline substitution as the part of optimisation. So there is no function call present (foo) inside main. Please check below assembly code which compiler will generate.
Normally GCC’s file scope is “not extern linkage”. That means inline function is never ever provided to the linker which is causing linker error, mentioned above.
How to remove this error?
To resolve this problem use “static” before inline. Using static keyword forces the compiler to consider this inline function in the linker, and hence the program compiles and run successfully.
Output is: 2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Inline namespaces and usage of the "using" directive inside namespaces
- Displaying inline and multiline blocks of code using Bootstrap
- Difference between Virtual function and Pure virtual function in C++
- How to call some function before main() function in C++?
- How to call function within function in C or C++
- Write a one line C function to round floating point numbers
- Does C support function overloading?
- How can I return multiple values from a function?
- What is the purpose of a function prototype?
- How to declare a pointer to a function?
- C function to Swap strings
- Can we call an undeclared function in C++?
- What is evaluation order of function parameters in C?
- Can we use function on left side of an expression in C and C++?
- Function Pointer in C
- Importance of function prototype in C
- What happens when a function is called before its declaration in C?
- Default arguments and virtual function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.