开发者

Why do I explicitly have to declare functions inline here?

Okay, up until now, I thought that functions defined in header files are treated like inline functions, just like template stuff, defined once, and all that.

I also use inclusion guards, yet I still got linker errors of multiple defined objects, and I know that is because of all those different units duplicating stuff the linker tries to pick out which item is the right one.

I also know that inline is merely a suggestion, and might not even get used by the compiler, etc.

Yet I have to explicitly define all those small functions in that little header only toolset I wrote.

Even if the functions were huge, I'd have to declare them inline, and the compiler would still possibly disregard the hint.

Yet I have to define them so anyway.

Example:

#ifndef texture_math_h__
#define texture_math_h__

float TexcoordToPixel(float coord, float dimension)
{
    return coord * dimension;
}

float PixelToTexcoord(float pixel, float dimension)
{
    return pixel / dimension;
}

float RecalcTexcoord(float coord,float oldDimension, float newDimension)
{
    return PixelToTexcoord(TexcoordToPixel(coord,oldDimension),newDimension);
}
#endif // texture_math_h__

Errors are , blabla already defined 开发者_高级运维in xxx.obj, for each unit that includes the file

When I declare all of those inline, it links correctly.

What's the reason for that? It's not a huge problem, and heck, optimizations probably inline stuff found in cpp, too, right?

I'm just curious about the why here, hope it's not too much of a duplicate and thank you for your time.


Inclusion guards only guard against the code being included twice in the same translation unit. So if you have multiple files that include the same header, the code is included multiple times. Functions defined in a header are not inline by default, so this will give you linker errors - you need to define those function with the inline keyword, unless they are member functions of a class.

Also, note that names that contain double-underscores are reserved for the purposes of the C++ implementation - you are not allowed to create such names in your own code.


Member functions are potencially inlined - you can't force inlining! - if (a) they are defined inside the class or if (b) you use the inline-clause in the definition. Note if you are using the inline-clause you shouldn't define the function in the header - the only exception would be templates as these are "special".
As you just updated the question:
Every user of this header will have a definition of the functions -> multiple defintions. You need to separate definition and declaration!


It's all about the one definition rule. This states that you can only have one definition for each non-inline function (along with various other types of entity) in a C++ program across all the translation units that you link in to make the program.

Marking a function inline enables an exception to the usual one definition rule. It states (paraphrased) that you can have one definition of an inline function per translation unit provided that all the definitions match and that a definition is provided in each translation in which the inline function is used.

Include guards will prevent you from accidentally providing more than one definition per translation unit by including the header file containing the definitions multiple times.

To satisfy the one definition rule for a non-inline function you would still have to ensure that there is only one translation unit containing the function definitions. The usualy way to do this is by only declaring the functions in the header file and having a single source file containing the definitions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜