Calling method in the GNU Scientific Library

5 次查看(过去 30 天)
I want to call a function in the GNU Scientific Library (GSL) from MATLAB. In particular, I want to call this method:
int gsl_linalg_SV_decomp(gsl_matrix * A, gsl_matrix * V, gsl_vector * S, gsl_vector * work)
I have the following folder structure
.
├── Makefile
├── call_gsl_svd.m
├── include
│ ├── calculate_svd_example.h
│ └── run_svd.h
├── project
└── src
├── calculate_svd_example.c
├── project.c
└── run_svd.c
I can call make and the project builds without error. I can then call ./project and a call is made to the gsl_linalg_SV_decomp function and runs fine. calculate_svd_example.c has one simple method, calculate_svd_example, that takes no input and returns no output.
I want to call this simple method from MATLAB (as a first step, anyway). My call_gsl_svd.m reads:
function [] = call_gsl_svd()
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
%#codegen
% for code generation, preinitialize the output variable
% data type, size, and complexity
% generate an include in the C code
coder.cinclude('./include/calculate_svd_example.h');
% evaluate the C function
coder.ceval('./src/calculate_svd_example');
end
I then run this command:
>> codegen call_gsl_svd calculate_svd_example.c
I get this error:
[1/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c /panfs/roc/msisoft/matlab/R2019a/extern/version/c_mexapi_version.c -o build/glnxa64/c_mexapi_version.o
[2/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c call_gsl_svd.c -o build/glnxa64/call_gsl_svd.o
[3/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c call_gsl_svd_initialize.c -o build/glnxa64/call_gsl_svd_initialize.o
[4/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c interface/_coder_call_gsl_svd_api.c -o build/glnxa64/_coder_call_gsl_svd_api.o
[5/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c call_gsl_svd_data.c -o build/glnxa64/call_gsl_svd_data.o
[6/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c call_gsl_svd_terminate.c -o build/glnxa64/call_gsl_svd_terminate.o
[7/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c interface/_coder_call_gsl_svd_mex.c -o build/glnxa64/_coder_call_gsl_svd_mex.o
[8/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c /panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src/calculate_svd_example.c -o build/glnxa64/calculate_svd_example.o
[9/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c interface/_coder_call_gsl_svd_info.c -o build/glnxa64/_coder_call_gsl_svd_info.o
[10/10] /bin/gcc build/glnxa64/call_gsl_svd_data.o build/glnxa64/call_gsl_svd_initialize.o build/glnxa64/call_gsl_svd_terminate.o build/glnxa64/call_gsl_svd.o build/glnxa64/_coder_call_gsl_svd_info.o build/glnxa64/_coder_call_gsl_svd_api.o build/glnxa64/_coder_call_gsl_svd_mex.o build/glnxa64/calculate_svd_example.o build/glnxa64/c_mexapi_version.o -pthread -Wl,--no-undefined -Wl,-rpath-link,/panfs/roc/msisoft/matlab/R2019a/bin/glnxa64 -shared -L"/panfs/roc/msisoft/matlab/R2019a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o call_gsl_svd_mex.mexa64 -lemlrt -lcovrt -lut -lmwmathutil -Wl,--version-script,call_gsl_svd_mex.map
FAILED: call_gsl_svd_mex.mexa64
/bin/gcc build/glnxa64/call_gsl_svd_data.o build/glnxa64/call_gsl_svd_initialize.o build/glnxa64/call_gsl_svd_terminate.o build/glnxa64/call_gsl_svd.o build/glnxa64/_coder_call_gsl_svd_info.o build/glnxa64/_coder_call_gsl_svd_api.o build/glnxa64/_coder_call_gsl_svd_mex.o build/glnxa64/calculate_svd_example.o build/glnxa64/c_mexapi_version.o -pthread -Wl,--no-undefined -Wl,-rpath-link,/panfs/roc/msisoft/matlab/R2019a/bin/glnxa64 -shared -L"/panfs/roc/msisoft/matlab/R2019a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o call_gsl_svd_mex.mexa64 -lemlrt -lcovrt -lut -lmwmathutil -Wl,--version-script,call_gsl_svd_mex.map
build/glnxa64/calculate_svd_example.o: In function `calculate_svd_example':
calculate_svd_example.c:(.text+0x73): undefined reference to `gsl_matrix_view_array'
calculate_svd_example.c:(.text+0x82): undefined reference to `gsl_matrix_alloc'
calculate_svd_example.c:(.text+0x94): undefined reference to `gsl_matrix_alloc'
calculate_svd_example.c:(.text+0xa1): undefined reference to `gsl_vector_alloc'
calculate_svd_example.c:(.text+0xae): undefined reference to `gsl_vector_alloc'
calculate_svd_example.c:(.text+0xbc): undefined reference to `gsl_matrix_transpose_memcpy'
calculate_svd_example.c:(.text+0xcd): undefined reference to `gsl_linalg_SV_decomp'
calculate_svd_example.c:(.text+0xe1): undefined reference to `pretty_print'
calculate_svd_example.c:(.text+0xf5): undefined reference to `pretty_print_vector'
calculate_svd_example.c:(.text+0x109): undefined reference to `pretty_print'
calculate_svd_example.c:(.text+0x111): undefined reference to `gsl_matrix_free'
calculate_svd_example.c:(.text+0x119): undefined reference to `gsl_matrix_free'
calculate_svd_example.c:(.text+0x121): undefined reference to `gsl_vector_free'
calculate_svd_example.c:(.text+0x129): undefined reference to `gsl_vector_free'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
------------------------------------------------------------------------
??? Build error: C compiler produced errors. See the Build Log for further details.
More information
Code generation failed: View Error Report
Error using codegen
How do I do this call this simple C method? Do the .c, .h. and .m files all have to be in the same directory? How do I link with the GSL libraries?
(I have attached the source files as a ZIP file.)
  7 个评论

请先登录,再进行评论。

采纳的回答

Paul Reiners
Paul Reiners 2021-3-9
>> mex -v -L/home/reine097/c-libs/lib/ -R2017b run_svd.c calculate_svd_example.c -lgsl -lgslcblas -lm

更多回答(1 个)

Jan
Jan 2021-3-2
编辑:Jan 2021-3-2
You can create a C++-Mex function and call the library throught it. Unfortunately all inputs must be transposed, because Matlab uses the columnwise storing in opposite to GSL. But the rest of the conversion from Matlab arrays to GSL matrices is easy.
  1 个评论
Paul Reiners
Paul Reiners 2021-3-9
Hi Jan,
I added more details to my description, including attaching the source code files. If you could take another look, that would be great.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by