I can't make sense of the following behavior: one header with some basic types, and another header in which I use these types in several functions. Afterward I started constructing classes based on my defined types and functions. In the function header if I leave the following signature:
void whateverFunction(parameters)
The linker points out that there are multiple definitions of whateverFunction. Now if change it to:
inline void whateverFunction(parameters)
the linkage problem is gone and all compiles and links well. What I know concerning inline is that it replaces every function call with it's code other than that it's a pretty dark, so my question is:
How does the linker treats inline functions in C++?
The linker may not see inline functions at all. They are usually compiled straight into the code that calls them (i.e., the code is used in place of a function call).
If the compiler chooses not to inline the function (since it is merely a hint), I'm not sure, but I think the compiler emits it as a normal non-inline function and somehow annotates it so the linker just picks the first copy it sees and ignores the others.
When the function in the header is not inline, then multiple definitions of this function (e.g. in multiple translation units) is a violation of ODR rules.
Inline functions by default have external linkage. Hence, as a consequence of ODR rules (given below), such multiple definitions (e.g. in multiple translation units) are Okay:
How the linker treats inline functions is a pretty much implementation level detail. Suffice it to know that the implementation accepts such mulitple defintions within the limitations of ODR rules
Note that if the function declaration in header is changed to 'static inline....', then the inline function explicitly has internal linkage and each translation unit has it's own copy of the static inline function.
The inline just masks the problem. Having multiple definition points out a problem somewhere.
Juste be careful about how you use your headers. Dont forget to : - <<
#ifndef HEADER_NAME
/#define HEADER_NAME
/#endif
>> to avoid multiple inclusion. - Do not use indirect inclusion : if you use a type in a file, add the corresponding header, even if another header in the same file includes it.