I built a static lib to call a matlab function from C++. when i try to use it i
get many undefined referece errors. i had previously built a shared lib from the same source & it worked fine
my codes for the static lib(call_matlab_processing.a) generation:
call_matlab_processing.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#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;
}
|
call_matlab_processing.h
|
int call_matlab_processing(double array[],int sorted_indices[], int size_array);
|
cmakelists.txt for static lib generation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(call_matlab_processing)
set(CMAKE_BUILD_TYPE Release)
set (PROJECT_LINK_LIBS libeng.so libmat.so libmx.so libut.so)
link_directories(/usr/local/MATLAB/R2014a/bin/glnxa64)
include_directories(/usr/local/MATLAB/R2014a/extern/include/ include)
add_library(call_matlab_processing STATIC src/call_matlab_processing.cpp)
target_link_libraries (call_matlab_processing ${PROJECT_LINK_LIBS})
|
when i use the created static lib in main i get undefined refernce errors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#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,127, 93, 1, 26, 58, 74 };
int sorted_indices[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;
}
|
my CmakeLists.txt for main
1 2 3 4 5 6 7 8 9
|
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(matlab_sort_using_static)
set (PROJECT_LINK_LIBS libcall_matlab_processing.a)
link_directories(~/temp_codes/important_codes/build)
include_directories(~/temp_codes/important_codes/include)
add_executable (matlab_sort_using_static matlab_sort_using_static.cpp)
target_link_libraries (matlab_sort_using_static ${PROJECT_LINK_LIBS})
|
errors:
Building CXX object CMakeFiles/matlab_sort_using_static.dir/matlab_sort_using_static.cpp.o
/usr/bin/cmake: /usr/local/MATLAB/R2014a/bin/glnxa64/libcurl.so.4: no version information available (required by /usr/bin/cmake)
Linking CXX executable matlab_sort_using_static
/usr/bin/cmake: /usr/local/MATLAB/R2014a/bin/glnxa64/libcurl.so.4: no version information available (required by /usr/bin/cmake)
/home/shome/temp_codes/important_codes/build/libcall_matlab_processing.a(call_matlab_processing.cpp.o): In function `call_matlab_processing(double*, int*, int)':
call_matlab_processing.cpp:(.text+0x1d): undefined reference to `engOpen'
call_matlab_processing.cpp:(.text+0x43): undefined reference to `mxCreateDoubleMatrix'
call_matlab_processing.cpp:(.text+0x4f): undefined reference to `mxGetPr'
call_matlab_processing.cpp:(.text+0x6e): undefined reference to `engPutVariable'
call_matlab_processing.cpp:(.text+0x7b): undefined reference to `engEvalString'
call_matlab_processing.cpp:(.text+0x88): undefined reference to `engEvalString'
call_matlab_processing.cpp:(.text+0x95): undefined reference to `engEvalString'
call_matlab_processing.cpp:(.text+0xa2): undefined reference to `engEvalString'
call_matlab_processing.cpp:(.text+0xaf): undefined reference to `engGetVariable'
call_matlab_processing.cpp:(.text+0xc1): undefined reference to `engGetVariable'
call_matlab_processing.cpp:(.text+0xd0): undefined reference to `mxGetData'
call_matlab_processing.cpp:(.text+0xdf): undefined reference to `mxGetData'
call_matlab_processing.cpp:(.text+0x278): undefined reference to `mxDestroyArray'
call_matlab_processing.cpp:(.text+0x282): undefined reference to `mxDestroyArray'
call_matlab_processing.cpp:(.text+0x28c): undefined reference to `mxDestroyArray'
call_matlab_processing.cpp:(.text+0x299): undefined reference to `engEvalString'
call_matlab_processing.cpp:(.text+0x2a1): undefined reference to `engClose'
collect2: error: ld returned 1 exit status
make[2]: *** [matlab_sort_using_static] Error 1