chappjc/MATLAB

mex cppClass link errors

Closed this issue · 2 comments

njman commented

Hi,

I want to mex class_wrapper_template.cpp in MATLAB command window, get the class_wrapper_template.mexw32. But it failed.

>> mex -v -g class_wrapper_template.cpp
......
......
Error using mex
   Creating library class_wrapper_template.lib and object class_wrapper_template.exp
class_wrapper_template.obj : error LNK2019: unresolved external symbol "public: __thiscall
PQheap<double>::PQheap<double>(void)" (??0?$PQheap@N@@QAE@XZ) referenced in function "public:
__thiscall std::_Ref_count_obj<class PQheap<double> >::_Ref_count_obj<class PQheap<double> ><>(void)"
(??$?0$$$V@?$_Ref_count_obj@V?$PQheap@N@@@std@@QAE@XZ)
class_wrapper_template.obj : error LNK2019: unresolved external symbol "public: __thiscall
PQheap<double>::~PQheap<double>(void)" (??1?$PQheap@N@@QAE@XZ) referenced in function "public: void *
__thiscall PQheap<double>::`scalar deleting destructor'(unsigned int)" (??_G?$PQheap@N@@QAEPAXI@Z)
class_wrapper_template.obj : error LNK2019: unresolved external symbol "public: int __thiscall
PQheap<double>::init(unsigned int)" (?init@?$PQheap@N@@QAEHI@Z) referenced in function _mexFunction
class_wrapper_template.obj : error LNK2019: unresolved external symbol "public: __thiscall
PQheap<double>::PQheap<double>(unsigned int)" (??0?$PQheap@N@@QAE@I@Z) referenced in function
"public: __thiscall std::_Ref_count_obj<class PQheap<double> >::_Ref_count_obj<class PQheap<double>
><unsigned int &>(unsigned int &)" (??$?0AAI@?$_Ref_count_obj@V?$PQheap@N@@@std@@QAE@AAI@Z)
class_wrapper_template.obj : error LNK2019: unresolved external symbol "public: int __thiscall
PQheap<double>::insert(double const &)" (?insert@?$PQheap@N@@QAEHABN@Z) referenced in function
_mexFunction
class_wrapper_template.obj : error LNK2019: unresolved external symbol "public: double __thiscall
PQheap<double>::extractTop(void)" (?extractTop@?$PQheap@N@@QAENXZ) referenced in function
_mexFunction
class_wrapper_template.mexw32 : fatal error LNK1120: 6 unresolved externals

Not sure where the link errors came when template was involved?

thanks

As noted in documentation in pqheap.hpp, the implementation file (cpp) is not included so link errors are to be expected.

The idea is to replace pqheap.hpp and the object PQheap with your own C++ class. However, if you would just like this for demonstration purposes, I can see if there is a cpp file that goes with this.

Actually, you could use the full priority queue implementation from my other repository: https://github.com/chappjc/PriorityQueue/tree/master/include

I've added the .cpp and fixed some small issues. The cppClass sample should be fully functional now:

>> pq = pqheap(6)
pq = 
  pqheap with no properties.
>> pq.capacity
ans =
     6
>> pq.insert(3)
ans =
     1
>> pq.insert(2)
ans =
     2
>> pq.insert(8)
ans =
     1
>> pq.insert(4)
ans =
     2
>> pq.insert(8)
ans =
     2
>> pq.insert(3)
ans =
     6
>> pq.insert(-30)
ans =
     5
>> pq.printHeap
| 8 | 4 | 3 | 2 | -30 | 3 |
>> pq.extract
ans =
     8
>> pq.extract
ans =
     4
>> pq.printHeap
| 3 | 2 | 3 | -30 |
>> pq.len
ans =
     4
>> pq.capacity
ans =
     6

But this was not so much the point of cppClass as the ability to use any persistent C++ object.