created a shared library(.so ) using matlab engine but unable to call it from a program
Afficher commentaires plus anciens
i created a cpp file to call a sorting function in matlab through matlab engine. i built it as a .so file. The library files & the main are attached herewith
call_matlab_processing.h
int call_matlab_processing(double array[],int sorted_indices[], int size_array);
call_matlab_processing.cpp
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#include <iostream>
int call_matlab_processing(double array[],int sorted_indices[], int size_array)
{
// its just a sorting algo, sorting being implemented by matlab
double * sorted;
double *indices;
//
Engine *ep;
mxArray *T = NULL, *result = NULL, *indices_matlab=NULL;
if (!(ep = engOpen(""))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return 1;
}
T = mxCreateDoubleMatrix(1,size_array, mxREAL);
memcpy((void *)mxGetPr(T), (void *)array, size_array*sizeof(double));
//
engPutVariable(ep, "T", T);
engEvalString(ep, "[D,I] = sort(T,'descend')");
engEvalString(ep, ";");
engEvalString(ep, "figure,plot(D);");
engEvalString(ep, "dlmwrite('myFile.txt',I)");
result = engGetVariable(ep,"D");
indices_matlab = engGetVariable(ep,"I");
//
sorted=(double *)mxGetData(result);
indices=(double *)mxGetData(indices_matlab);// casting as int * gave wrong results
//
for(int i = 0; i < size_array;i++)
{
array[i]=*sorted;
sorted_indices[i]=*indices;
sorted++;
indices++;
}
//
int goahead=fgetc(stdin);
//
mxDestroyArray(T);
mxDestroyArray(result);
mxDestroyArray(indices_matlab);
//
engEvalString(ep, "close;");
engClose(ep);
//
return 0;
}
so: libcall_matlab_processing
main.cpp:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#define BUFSIZE 256
#include <call_matlab_processing.h>
int main()
{
int size_array=10;
//
double array[10]={ 10, 5, 12, 27, 93, 19, 1, 26, 58, 74 };
int sorted_indices[10]={1,2,3,4,5,6,7,8,9,10};
//
int result=call_matlab_processing(array,sorted_indices,10);
for (int i=0;i<10;i++)
{
std::cout << "sorted_array= " <<array[i]<< " indices="<< sorted_indices[i] <<std::endl;
}
//
std::cout << "Done"<< std::endl;
return EXIT_SUCCESS;
}
main.cpp calls the above .so file. when i tried to build main i got so many errors: bin/ld: warning: libut.so, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libeng.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libmat.so, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libeng.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libmwcpp11compat.so, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libeng.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libmwresource_core.so, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libmwi18n.so, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libmwfl.so, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libmwMATLAB_res.so, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libboost_date_time.so.1.49.0, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libboost_signals.so.1.49.0, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libboost_system.so.1.49.0, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libboost_thread.so.1.49.0, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libboost_log.so.1.49.0, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libboost_log_setup.so.1.49.0, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libicudata.so.49, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) make[2]: Leaving directory `/home/shome/temp_codes/matlab_cpp_interface_codes/matlab_sort_using_lib/build' /usr/bin/ld: warning: libicuuc.so.49, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libicui18n.so.49, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libicuio.so.49, needed by //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so, not found (try using -rpath or -rpath-link) //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::catenate::emptyArrayColumnsDimensionMismatch::emptyArrayColumnsDimensionMismatch()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `fl::except::IUserException::ToUstringImpl() const' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::legacy_two_part::badformat_mx::badformat_mx()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `resource_core::BaseMsgID::getName() const' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::legacy_two_part::subsassignnumelmismatch::subsassignnumelmismatch()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `ucnv_fromUChars_49' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `ucnv_getNextUChar_49' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `ut_ghash_query' make[1]: Leaving directory `/home/shome/temp_codes/matlab_cpp_interface_codes/matlab_sort_using_lib/build' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `fl::i18n::to_ustring(char const*)' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::legacy_two_part::badopt::badopt()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `utLogWarning(utWarningManagerContext*, std::string const&, std::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t> > const&, __va_list_tag*, std::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t> > const&, bool)' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `typeinfo for fl::except::IUserException' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MathWorks::I18N::FromUTF16Converter::FromUTF16Converter(char16_t const*, int, char const*)' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `fl::filesystem::get_install_path()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::legacy_two_part::nocomplex::nocomplex()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::checkDimCommon::nonnegativeSize::nonnegativeSize()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::checkDimCommon::complexSize::complexSize()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::matrix::accessBadTypeIndices::accessBadTypeIndices(std::string const&, std::string const&)' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::legacy_two_part::Serialize::Serialize(std::string const&, std::string const&)' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `typeinfo for fl::except::IException' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `utf16_strlen_lim' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `utf16_strcpy' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::colon::logicalInput::logicalInput()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::catenate::opaqueDimensionMismatch::opaqueDimensionMismatch()' //usr/local/MATLAB/R2014a/bin/glnxa64/libeng.so: undefined reference to `utF2cstr' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::matrix::moreSubsAssignDimMismatch::moreSubsAssignDimMismatch()' //usr/local/MATLAB/R2014a/bin/glnxa64/libmx.so: undefined reference to `MATLAB::Cell2Str
3 commentaires
Titus Edelhofer
le 19 Nov 2015
Maybe you could comment on the answers given to you in the original question before posting new ones ...?
shome
le 19 Nov 2015
Titus Edelhofer
le 19 Nov 2015
No problem. I've formatted the question so that it's easier to read.
Réponses (0)
Catégories
En savoir plus sur C Shared Library Integration dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!