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)?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With