mex errors when compiling c code; however the same code compiles perfectly in Mac Cmd line.
显示 更早的评论
For background:
I have a large simulation that I have built in Matlab, and I would like to incorporate a C-based model as a subroutine. To be clear, I need to run some MATLAB code, then the C code, then MATLAB again. The C code is rather complex, living in its own 84-file folder.
I can successfully compile that C code in the Mac terminal via
make cModel
and the compiled code runs as expected. But I now need to get MATLAB to run that C code , and it is here that I am struggling.
The MATLAB bit:
My understanding is that, to get C code that I can run from MATLAB via coder.ceval, I need to compile the C model via the mex command (I've tested that the terminal-compiled C does not run when called by coder.ceval - please correct me if I'm wrong!).
Compiling with
mex cModel
gives
Error using mex
Undefined symbols for architecture x86_64:
"_derivs", referenced from:
_main in cModel.o
etc... (another 10 C functions considered undefined)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Which seems odd, as my c compiler is perfectly happy with cModel when not called from MATLAB. Any help on how to fix this error would be much appreciated!
Edit: I should probably add that I know I could blindly add in the names of extra C files in the cModel folder to the mex command; although that would mean adding up to 80 filenames with unclear priority/ordering, which C does care about... Regardless, the main code cModel.c does contain the 'include' commands necessary for a normal c compiler to successfully compile - clearly there is something I really don't understand about mex that is causing this compile problem.
Edit2: Blindly adding all C files to mex removes the above errors (no idea why) and replaces them with
Error using mex
Undefined symbols for architecture x86_64:
"_mexFunction", referenced from:
-exported_symbol[s_list] command line option
which I'm trying to solve now.
Many thanks, Jonathan
PS. Verbose compile attached (for R2014b Mac, run inside the folder containing cModel)
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File 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!