Matlab mex cannot include Opensim.h
Matlab mex cannot include Opensim.h
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.
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.
Tags:
- Dimitar Stanev
- Posts: 1096
- Joined: Fri Jan 31, 2014 5:14 am
Re: Matlab mex cannot include Opensim.h
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
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
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
https://stackoverflow.com/questions/105 ... in-unicodeThe file contains a character that cannot be represented in the current code page (936).
Re: Matlab mex cannot include Opensim.h
Thank you! It does help to remove the warning, but the errors still remain.jimmy wrote: ↑Sat Nov 09, 2019 6:01 pmhttps://stackoverflow.com/questions/105 ... in-unicodeThe file contains a character that cannot be represented in the current code page (936).
Re: Matlab mex cannot include Opensim.h
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.
- Nicos Haralabidis
- Posts: 197
- Joined: Tue Aug 16, 2016 1:46 am
Re: Matlab mex cannot include Opensim.h
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
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
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.
- Dimitar Stanev
- Posts: 1096
- Joined: Fri Jan 31, 2014 5:14 am
Re: Matlab mex cannot include Opensim.h
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
https://stackoverflow.com/questions/676 ... es#6768067
https://www.mathworks.com/help/matlab/r ... d1a18985c6