Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline function linker error

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:

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:

  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)?

like image 977
stanigator Avatar asked Jun 05 '09 00:06

stanigator


People also ask

How do you fix a linker error?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

Can linker inline functions?

The linker can inline small functions in place of a branch instruction to that function. For the linker to be able to do this, the function (without the return instruction) must fit in the four bytes of the branch instruction.

What causes a linker error?

Linker errors occur when the linker is trying to put all the pieces of a program together to create an executable, and one or more pieces are missing. Typically, this can happen when an object file or libraries can't be found by the linker.

What causes linker errors C++?

Linker Errors: These error occurs when after compilation we link the different object files with main's object using Ctrl+F9 key(RUN). These are errors generated when the executable of the program cannot be generated. This may be due to wrong function prototyping, incorrect header files.


1 Answers

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.

like image 117
Nikolai Fetissov Avatar answered Oct 18 '22 09:10

Nikolai Fetissov