How to use external C library in Codegen process to generate mex file

5 views (last 30 days)
I'm trying to use the CSparse/CXSParse toolbox in a Matlab coder project.
Here is a sample code i want to compile. C (only) code is generated, but linking fails, so i cannot get mex files from it or test the generated code in matlab.
Here is my command : codegen -config:mex testCSparse
The function :
function [OUT] = testCSparse %# codegen
%This function generate C code to test input et output
% variable types uses by CSparse Library
v1 = round(rand(10,1)*100);
v2 = round(rand(10,1)*100);
v3 = 1:10;
if coder.target('MATLAB') % for matlab 2014
OUT = cs_sparse(v1,v2,v3); % here a mexed C func is called
else
OUT = struct;
OUT = coder.ceval('cs_sparse',v1,v2,v3);
% how to use this struct OUT in matlab : newMat = OUT generates an error..
end
It leads to an error. The codegen command allows me "Generate code only" without errors, if i select a mex output it doesn't work. So i cannot test it on Matlab.
---------------- Error Generated :
Build Log 1 xcrun -sdk macosx10.10 clang -c -fno-common -arch x86_64 -isysroot [•••]
-I "/Applications/MATLAB_R2014a.app/extern/include" -I "." "testCSparse.c"
5 testCSparse.c:43:3: warning: implicit declaration of
6 function 'cs_sparse' is invalid in C99
7 [-Wimplicit-function-declaration]
8 cs_sparse(v1, v2, dv0);
9 ^
10 1 warning generated.
11 xcrun -sdk macosx10.10 clang -c -fno-common -arch x86_64 -isysroot
14 xcrun -sdk macosx10.10 clang -Wl,-exported_symbols_list,testCSparse_mex_mex.map -arch x86_64 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -mmacosx-version-min=10.10 -bundle -O -o testCSparse_mex.mexmaci64 testCSparse_data.o testCSparse_initialize.o testCSparse_terminate.o testCSparse.o _coder_testCSparse_api.o _coder_testCSparse_mex.o _coder_testCSparse_info.o -L/Applications/MATLAB_R2014a.app/bin/maci64 -lmx -lmex -lmat -lstdc++ -lemlrt -lcovrt -lut -lmwmathutil -lmwblas
15 Undefined symbols for architecture x86_64:
16 "_cs_sparse", referenced from:
17 _testCSparse in testCSparse.o
18 ld: symbol(s) not found for architecture x86_64
19 clang: error: linker command failed with exit code 1 (use -v to see invocation)
20 gmake: *** [testCSparse_mex.mexmaci64] Error 1

Answers (1)

Raghu Boggavarapu
Raghu Boggavarapu on 26 Nov 2021
You also need to include the header file that declares cs_sparse function using coder.cinclude and link the library that defines it using coder.updateBuildInfo to avoid build failure.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by