The special rules for inline functions require that they be defined in each file in which
they are used.The easiest way to make sure that inline definitions are available to all files
in a multifile program is to include the inline definition in the same header file in which
the corresponding class is defined.

please write a sample code to make me understand the above point.

2 Years
Discussion Span
Last Post by L7Sqr

To fully understand this you need to understand how stages of compilation work over several files. However, conceptually, you can think of this using the following example:

File: main.c

int add (int x, int y) {
   return x + y; 

int main () {
   int i = 3, j = 7; 
   int result = add (i, j);
   return result;

Now, when you inline the add function you can think of the the body of the function being placed at the call site instead of being defined separately from the call site (as above). In other words, you get something like:

int main () {
   int i = 3, j = 7;
   int result = 0;
   int tmp = i + j;
   result = tmp;
   return result;

Notice how there is no longer an add function to call; the functionality is now place inline in the calling function.

This is fine if everything is placed in the same compilation unit (i.e. file) but if you are expecting to call add from some other source file (foo.c, for example) but it gets inlined in main.c there is no longer anything to call and the code in foo.c will fail to resolve a symbol.

Now, the way #include works is that is copies code from the included file into the file you are writing. This happens before the inlining phase of compilation. Therefore, if you provide a definition to a function in a header file and include that definition in each source that uses the definition there will never be an failure to resolve an external symbol (because a local definition will be found and inlined).

Edited by L7Sqr

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.