Page 1 of 2

Matlab mex cannot include Opensim.h

Posted: Wed Nov 06, 2019 10:44 pm
by wli45
Hi everyone,

I'm writing a mex file for testing. I use Matlab 2019a with Opensim 4.0, and MinGW64 Compiler (C++). My system is 64-bit win10.
I just use a MATLAB example code, but include an OpenSim.h . The code is like:


#include "mex.hpp"
#include "mexAdapter.hpp"
#include "OpenSim\OpenSim.h"

using namespace matlab::data;
using matlab::mex::ArgumentList;

class MexFunction : public matlab::mex::Function {
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
const double offSet = inputs[0][0];
TypedArray<double> doubleArray = std::move(inputs[1]);
for (auto& elem : doubleArray) {
elem += offSet;
}
outputs[0] = doubleArray;
}
};


And I use matlab command to compile:
mex myfile.cpp...
-L'C:\Program Files\OpenSim 4.0\sdk\lib'...
-L'C:\Program Files\OpenSim 4.0\sdk\Simbody\lib'...
-losimCommon -losimSimulation...
-losimAnalyses -losimActuators -losimTools...
-lSimTKcommon -lSimTKmath -lSimTKsimbody...
-I'C:/Program Files/OpenSim 4.0/sdk/include'...
-I'C:/Program Files/OpenSim 4.0/sdk'...
-I'C:/Program Files/OpenSim 4.0/sdk/Simbody/include'...
-I'C:/Program Files/OpenSim 4.0/sdk/include/Opensim'...
;


The I get a lot of errors like:
C:\Users\username\AppData\Local\Temp\mex_43450185476353_8508\myfile.obj:myfile.cpp:(.text$_ZNK7OpenSim9ArrayPtrsINS_6ObjectEE7getLastEv[_ZNK7OpenSim9ArrayPtrsINS_6ObjectEE7getLastEv]+0x6b):
undefined reference to
`__imp__ZN7OpenSim9ExceptionC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_i'
collect2.exe: error: ld returned 1 exit status


I don't have solid knowledge in C++. Can someone help? Thanks in advance.

Re: Matlab mex cannot include Opensim.h

Posted: Thu Nov 07, 2019 1:41 am
by mitkof6
I have the impression that OpenSim cannot be compiled with MinGW because there are some namespace conflicts. Can you try compiling an example to verify this. If this is the case, then you might need to use another compiler (Microsoft compiler).

Re: Matlab mex cannot include Opensim.h

Posted: Thu Nov 07, 2019 9:15 am
by wli45
mitkof6 wrote:
Thu Nov 07, 2019 1:41 am
I have the impression that OpenSim cannot be compiled with MinGW because there are some namespace conflicts. Can you try compiling an example to verify this. If this is the case, then you might need to use another compiler (Microsoft compiler).
Yes, I tried the example I posted without #include Opensim.h, and it works. I'll try other compilers. Thanks for your reply.

Re: Matlab mex cannot include Opensim.h

Posted: Thu Nov 07, 2019 6:21 pm
by wli45
mitkof6 wrote:
Thu Nov 07, 2019 1:41 am
I have the impression that OpenSim cannot be compiled with MinGW because there are some namespace conflicts. Can you try compiling an example to verify this. If this is the case, then you might need to use another compiler (Microsoft compiler).
I got this error, and still have no clue:
Building with 'Microsoft Visual C++ 2019'.
Error using mex
test1.cpp
C:\Program Files\OpenSim 4.0\sdk\Simbody\include\simmath/Optimizer.h(1): warning C4819:
The file contains a character that cannot be represented in the current code page (936).
Save the file in Unicode format to prevent data loss
C:\Program Files\OpenSim
4.0\sdk\include\OpenSim\Simulation\SimbodyEngine/EllipsoidJoint.h(1): warning C4819: The
file contains a character that cannot be represented in the current code page (936).
Save the file in Unicode format to prevent data loss
C:\Program Files\OpenSim 4.0\sdk\include\OpenSim\Common\Component.h(3129): error C2593:
'operator !=' is ambiguous
C:\Program Files\MATLAB\R2019b\extern\include\MatlabDataArray\Reference.hpp(250): note:
could be 'bool matlab::data::operator
!=<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>>(const
T1 &,const T2 &) noexcept'
with
[
_Ty=std::tuple<SimTK::ReferencePtr<const
OpenSim::Output<SimTK::Vector>>,std::string,std::string>,
T1=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::tuple<SimTK::ReferencePtr<const
OpenSim::Output<SimTK::Vector>>,std::string,std::string>>>>,
T2=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::tuple<SimTK::ReferencePtr<const
OpenSim::Output<SimTK::Vector>>,std::string,std::string>>>>
]
C:\Program Files\OpenSim 4.0\sdk\Simbody\include\SimTKcommon/internal/common.h(641):
note: or 'bool SimTK::operator
!=<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>>(const
L &,const R &)'
with
[
_Ty=std::tuple<SimTK::ReferencePtr<const
OpenSim::Output<SimTK::Vector>>,std::string,std::string>,
L=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::tuple<SimTK::ReferencePtr<const
OpenSim::Output<SimTK::Vector>>,std::string,std::string>>>>,
R=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::tuple<SimTK::ReferencePtr<const
OpenSim::Output<SimTK::Vector>>,std::string,std::string>>>>
]
C:\Program Files\OpenSim 4.0\sdk\include\OpenSim\Common\Component.h(3129): note: while
trying to match the argument list
'(std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,
std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>)'
with
[
_Ty=std::tuple<SimTK::ReferencePtr<const
OpenSim::Output<SimTK::Vector>>,std::string,std::string>
]
C:\Program Files\OpenSim 4.0\sdk\include\OpenSim\Common\Component.h(3120): note: while
compiling class template member function 'void
OpenSim::Input<SimTK::Vector>::finalizeConnection(const OpenSim::Component &)'
C:\Program Files\OpenSim 4.0\sdk\include\OpenSim\Common\Reporter.h(345): note: see
reference to class template instantiation 'OpenSim::Input<SimTK::Vector>' being compiled
C:\Program Files\OpenSim 4.0\sdk\include\OpenSim\Common\Component.h(3129): error C2088:
'!=': illegal for class


Thanks!

Re: Matlab mex cannot include Opensim.h

Posted: Sat Nov 09, 2019 6:01 pm
by jimmy
The file contains a character that cannot be represented in the current code page (936).
https://stackoverflow.com/questions/105 ... in-unicode

Re: Matlab mex cannot include Opensim.h

Posted: Sat Nov 09, 2019 10:30 pm
by wli45
jimmy wrote:
Sat Nov 09, 2019 6:01 pm
The file contains a character that cannot be represented in the current code page (936).
https://stackoverflow.com/questions/105 ... in-unicode
Thank you! It does help to remove the warning, but the errors still remain.

Re: Matlab mex cannot include Opensim.h

Posted: Mon Nov 11, 2019 7:22 am
by jimmy
This seems like a Matlab/compiler issue and not an OpenSim issue, so I would suggest looking at Stackoverflow or Matlab help sites to try and solve your issue.

Re: Matlab mex cannot include Opensim.h

Posted: Mon Nov 11, 2019 11:18 am
by nicos1993
Hi Wentao,

I have the Matlab-OpenSim mex functionality working for OpenSim 3.3. I seem to remember reading somewhere that you need to compile the Mex functions with the same compiler with which the version of OpenSim you're using... (I could be wrong...). The 3.3 version I have was built with VS 15 I believe, and my Matlab C compiler is for VS 15.

I hope that helps somehow!

Kind regards,

Nicos Haralabidis

Re: Matlab mex cannot include Opensim.h

Posted: Fri Nov 15, 2019 1:54 pm
by wli45
Thanks for your reply. I agree with you, and I checked with VS 15 and 17 that are used by Opensim 4.0, but neither works. I still cannot figure out the problem. But luckily I can use other methods to continue my work.

Re: Matlab mex cannot include Opensim.h

Posted: Fri Nov 15, 2019 3:44 pm
by mitkof6
Maybe you haven't provided the include dir when compiling with mex. The compiler must know the location of the OpenSim.h file directory. This is typically provided with the -I argument.

https://stackoverflow.com/questions/676 ... es#6768067
https://www.mathworks.com/help/matlab/r ... d1a18985c6