C++ 内联函数链接器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/953710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:06:56  来源:igfitidea点击:

inline function linker error

c++inlinelinker-errors

提问by stanigator

I am trying to use inline member functions of a particular class. For example the function declaration and implementation without inlining is as such:

我正在尝试使用特定类的内联成员函数。例如没有内联的函数声明和实现是这样的:

in the header file:

在头文件中:

int GetTplLSize();

in the .cpp file:

在 .cpp 文件中:

int NeedleUSsim::GetTplLSize()
{
    return sampleDim[1];
}

For some reason if I put the "inline" keyword in either one of the implementation and declaration, as well as in both places, I get linker errors as shown:

出于某种原因,如果我将“inline”关键字放在实现和声明之一以及两个地方,我会收到链接器错误,如下所示:

 Creating library C:\DOCUME~1\STANLEY\LOCALS~1\TEMP\MEX_HN~1\templib.x and object C:\DOCUME~1\STANLEY\LOCALS~1\TEMP\MEX_HN~1\templib.exp 
mexfunction.obj : error LNK2019: unresolved external symbol "public: int __thiscall NeedleUSsim::GetTplLSize(void)" (?GetTplLSize@NeedleUSsim@@QAEHXZ) referenced in function _mexFunction 
mexfunction.mexw32 : fatal error LNK1120: 1 unresolved externals 

  C:\PROGRA~1\MATLAB\R2008B\BIN\MEX.PL: Error: Link of 'mexfunction.mexw32' failed. 

What needs to be in order to get rid of this error (i.e. what am I doing wrong in terms of making these inline member functions)?

为了摆脱这个错误需要做什么(即我在制作这些内联成员函数方面做错了什么)?

回答by Nikolai Fetissov

You need to put function definition into the header then. The simplest way to hint the compiler to inline is to include method body in the class declaration like:

然后您需要将函数定义放入标题中。提示编译器内联的最简单方法是在类声明中包含方法体,例如:


class NeedleUSsim
{
  // ...
  int GetTplLSize() const { return sampleDim[1]; }
  // ...
};

or, if you insist on separate declaration and definition:

或者,如果您坚持单独声明和定义:


class NeedleUSsim
{
  // ...
  int GetTplLSize() const;
  // ...
};

inline int NeedleUSsim::GetTplLSize() const
{ return sampleDim[1]; }

The definition has to be visible in each translation unit that uses that method.

该定义必须在使用该方法的每个翻译单元中可见。

回答by young

from C++ FAQ Lite

来自 C++ FAQ Lite

If you put the inline function's definition into a .cpp file, and if it is called from some other .cpp file, you'll get an "unresolved external" error from the linker.

如果您将内联函数的定义放入一个 .cpp 文件中,并且如果它是从某个其他 .cpp 文件中调用的,您将收到来自链接器的“未解析的外部”错误。

How do you tell the compiler to make a member function inline?

你如何告诉编译器使成员函数内联?

回答by Ken Dyck

As others have already pointed out, you need to move the definition of the inlined function to the header file, like so:

正如其他人已经指出的那样,您需要将内联函数的定义移动到头文件中,如下所示:

class NeedleUSsim
{
  // ...
  inline int GetTplLSize() { return sampleDim[1]; }
  // ...
};

The reason for this is that the compiler needs to know what code to inline when it sees a call to the inlined function. If you leave the definition of the function in the .cpp file for the NeedleUSsim class, the code that the compiler generates for it becomes trapped in the the NeedleUSsim object file. As the compiler only reads source code—it never peeks into another class's object file—it simply has no way to know with what code to replace a call when it's compiling another .cpp file.

这样做的原因是编译器在看到对内联函数的调用时需要知道要内联哪些代码。如果将函数的定义保留在 NeedleUSsim 类的 .cpp 文件中,则编译器为它生成的代码将被困在 NeedleUSsim 对象文件中。由于编译器只读取源代码——它从不查看另一个类的目标文件——它根本无法知道在编译另一个 .cpp 文件时用什么代码来替换调用。

回答by Stephen Nutt

If you have an inline function you should put the definition in the header file.

如果你有一个内联函数,你应该把定义放在头文件中。

回答by grepsedawk

See the Inline Guard Macro idiom. This will at least allow you to separate, albeit slightly, the code from the declaration. It also allows you to toggle inlining of functions via a define.

请参阅内联保护宏习惯用法。这将至少允许您将代码与声明分开,尽管稍微分开。它还允许您通过define.