MATLAB - 编译 jpeg_read.c 以创建 mexmaci64 文件时出错

MATLAB - Error compiling jpeg_read.c to create mexmaci64 file

最近我将我的 Matlab 项目从 windows OS 移到了 Mac OS。所以我的 jpeg_read.mexw64 文件不再工作了,我需要创建一个与 Mac OS 兼容的新 mexmaci64 文件。 我从 here 下载了 JpegToolbox,然后使用:

安装了 Libjpeg
brew install libjpeg

在 Matlab 中我尝试使用 mex:

>> mex -setup
MEX configured to use 'Xcode with Clang' for C language compilation.

To choose a different language, select one from the following:
 mex -setup C++ 
 mex -setup FORTRAN
MEX configured to use 'Xcode Clang++' for C++ language compilation.
>> 

但是当我尝试时:

mex -I/usr/local/Cellar/jpeg/9d/include jpeg_read.c -L/usr/local/Cellar/jpeg/9d/lib

Matlab returns出现如下错误:

Building with 'Xcode with Clang'.
/Users/folder/jpeg_toolbox/jpeg_read.c:294:39: warning: incompatible pointer types passing 'int [2]' to parameter of type 'const mwSize *' (aka 'const unsigned long *') [-Wincompatible-pointer-types]
         mxtemp = mxCreateCharArray(2,dims);
                                      ^~~~
/Applications/Polyspace/R2020a/extern/include/matrix.h:958:91: note: passing argument to parameter 'dims' here
LIBMMWMATRIX_PUBLISHED_API_EXTERN_C mxArray *mxCreateCharArray(mwSize ndim, const mwSize *dims);
                                                                                          ^
1 warning generated.

Error using mex
Undefined symbols for architecture x86_64:
  "_jpeg_CreateDecompress", referenced from:
      _mexFunction in jpeg_read.o
  "_jpeg_destroy_decompress", referenced from:
      _mexFunction in jpeg_read.o
  "_jpeg_finish_decompress", referenced from:
      _mexFunction in jpeg_read.o
  "_jpeg_read_coefficients", referenced from:
      _mexFunction in jpeg_read.o
  "_jpeg_read_header", referenced from:
      _mexFunction in jpeg_read.o
  "_jpeg_save_markers", referenced from:
      _mexFunction in jpeg_read.o
  "_jpeg_std_error", referenced from:
      _mexFunction in jpeg_read.o
  "_jpeg_stdio_src", referenced from:
      _mexFunction in jpeg_read.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
 

现在如何编译程序?否则,你们中有人已经得到我需要的 mexmaci64 了吗?

当 MEX 文件使用 int 而不是 mwSize 作为数组大小时,通常会发生此错误。只有非常古老的 MEX 文件代码仍然这样做。

对于这些 MEX 文件,您需要在编译时将 -compatibleArrayDims 添加到 mex 命令中。它会导致编译器 select 旧的 32 位 API 而不是较新的 64 位之一。这确实限制了数组的最大大小,但仅限于与写入 MEX 文件时的限制一致的方式。

替代解决方案是重写 MEX 文件以使用更新的 API。