Ok, so after reading the VivienG link , I think I understood the exact reasoning for this error message. This is confusing and misleading (at least for me this should not happen if you have only one translation unit), but you can explain:
Assuming that the compiler does not really want to embed the code, he should know where to put it, especially when it is used in several translation units.
The classic approach is to create multiple copies, one for each translation unit (or at least for those units where it was used).
This can cause problems, for example. while trying to do some comparisons of function pointers (the question still remains why you should do this).
To counter this (and other issues that I might not have listed here), they thought of some pretty neat (although, as I mentioned, deceptive) solution:
You declare the function as inline as you know, but at the same time you tell the compiler where to put the non-inline version with the extern keyword.
So, in your example, you would save your as-is function and put it in the header file (so that it knows where it will be used):
inline int foo(void) { return 10 + 3; }
In addition, in order to tell the compiler where to place the non-linear version, you need to add one more forward declaration to one translation unit:
extern inline int foo(void);
Thus, the whole concept changes significantly compared to the classic functions: put the implementation in the header, and then a short declaration in only one file.
As already mentioned, when using the -O3 option, all code marked with inline is actually embedded, which will not cause a problem.
Mario
source share