MATLAB: MATLAB mex linking error using clang and external library fftw3

clangfftw3linkermex

My question is about linking errors I get when compiling with external libraries using mex. I'm trying to compile C++ code using the external library fftw3 using Matlab's mex on MacOS using clang++. Here is an example
file: main.cpp
#include "fftw3.h"
int main(){
double * test = (double *) fftw_malloc(10*sizeof(double));
}
compiling this works
$ clang++ main.cpp -L/usr/local/lib -lfftw3
Now I try something similar with mex
file: mex.cpp
#include "math.h"
#include "matrix.h"
#include "mex.h"
#include "fftw3.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double * test = (double *) fftw_malloc(10*sizeof(double));
}
compiling in MATLAB
>> mex -setup C++;
>> mex -L/usr/local/lib -lfftw3 CXXFLAGS='-L/usr/local/lib -lfftw3' mex.cpp
Building with 'Xcode Clang++'.
Error using mex
clang: warning: -lfftw3: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/local/lib'
/path/to/file/mex.cpp:4:10:
fatal error: 'fftw3.h' file not found
#include "fftw3.h"
^
1 error generated.
using the `-v` flag I get more detailed errors
Verbose mode is on.
Neither -compatibleArrayDims nor -largeArrayDims is selected.
Using -compatibleArrayDims. In the future, MATLAB will require the use of
-largeArrayDims and remove the -compatibleArrayDims option.
For more information:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
... Looking for compiler 'Xcode Clang++' ...
... Looking for environment variable 'DEVELOPER_DIR' ...No.
... Executing command 'xcode-select -print-path' ...Yes ('/Applications/Xcode.app/Contents/Developer').
... Looking for folder '/Applications/Xcode.app/Contents/Developer' ...Yes.
... Executing command 'which xcrun' ...Yes ('/usr/bin/xcrun').
... Looking for folder '/usr/bin' ...Yes.
... Executing command 'defaults read com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense' ...No.
... Executing command 'defaults read /Library/Preferences/com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense' ...Yes ('7.1.1').
... Executing command '
agreed=7.1.1
if echo $agreed | grep -E '[\.\"]' >/dev/null; then
lhs=`expr "$agreed" : '\([0-9]*\)[\.].*'`
rhs=`expr "$agreed" : '[0-9]*[\.]\(.*\)$'`
if echo $rhs | grep -E '[\."]' >/dev/null; then
rhs=`expr "$rhs" : '\([0-9]*\)[\.].*'`
fi
if [ $lhs -gt 4 ] || ( [ $lhs -eq 4 ] && [ $rhs -ge 3 ] ); then
echo $agreed
else
exit 1
fi
fi' ...Yes ('7.1.1').
... Executing command 'xcode-select -print-path' ...Yes ('/Applications/Xcode.app/Contents/Developer').
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk' ...Yes.
... Executing command 'xcode-select -print-path' ...Yes ('/Applications/Xcode.app/Contents/Developer').
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk' ...Yes.
... Executing command 'echo /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk | rev | cut -c1-10 | rev | egrep -oh '[0-9]+\.[0-9]+'' ...Yes ('10.11').
Found installed compiler 'Xcode Clang++'.
Options file details
-------------------------------------------------------------------
Compiler location: /Applications/Xcode.app/Contents/Developer
Options file: ~/.matlab/R2014b/mex_C++_maci64.xml
CMDLINE100 : /usr/bin/xcrun -sdk macosx10.11 clang++ -c -DMX_COMPAT_32 -DMATLAB_MEX_FILE -I"/Applications/MATLAB_R2014b.app/extern/include" -I"/Applications/MATLAB_R2014b.app/simulink/include" -L/usr/local/lib -lfftw3 -O2 -DNDEBUG /path/to/file/mex.cpp -o /var/folders/09/dysbhb1d5zn98sq8lzkwpn5r0000gn/T//mex_291104578803706_45753/mex.o
CMDLINE200 : /usr/bin/xcrun -sdk macosx10.11 clang++ -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.11 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -framework Cocoa -bundle -Wl,-exported_symbols_list,"/Applications/MATLAB_R2014b.app/extern/lib/maci64/mexFunction.map" -O -Wl,-exported_symbols_list,"/Applications/MATLAB_R2014b.app/extern/lib/maci64/mexFunction.map" /var/folders/09/dysbhb1d5zn98sq8lzkwpn5r0000gn/T//mex_291104578803706_45753/mex.o -lfftw3 -L/usr/local/lib -L"/Applications/MATLAB_R2014b.app/bin/maci64" -lmx -lmex -lmat -o mex.mexmaci64
CMDLINE300 : rm -f /var/folders/09/dysbhb1d5zn98sq8lzkwpn5r0000gn/T//mex_291104578803706_45753/mex.o
CXX : /usr/bin/xcrun -sdk macosx10.11 clang++
DEFINES : -DMX_COMPAT_32 -DMATLAB_MEX_FILE
MATLABMEX : -DMATLAB_MEX_FILE
CXXFLAGS : -L/usr/local/lib -lfftw3
INCLUDE : -I"/Applications/MATLAB_R2014b.app/extern/include" -I"/Applications/MATLAB_R2014b.app/simulink/include"
CXXOPTIMFLAGS : -O2 -DNDEBUG
CXXDEBUGFLAGS : -g
LDXX : /usr/bin/xcrun -sdk macosx10.11 clang++
LDFLAGS : -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.11 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -framework Cocoa -bundle -Wl,-exported_symbols_list,"/Applications/MATLAB_R2014b.app/extern/lib/maci64/mexFunction.map"
LDBUNDLE : -bundle
LINKEXPORT : -Wl,-exported_symbols_list,"/Applications/MATLAB_R2014b.app/extern/lib/maci64/mexFunction.map"
LINKLIBS : -lfftw3 -L/usr/local/lib -L"/Applications/MATLAB_R2014b.app/bin/maci64" -lmx -lmex -lmat
LDOPTIMFLAGS : -O
LDDEBUGFLAGS : -g
OBJEXT : .o
LDEXT : .mexmaci64
DEVELOPER_DIR_CHECK :
XCODE_DIR : /Applications/Xcode.app/Contents/Developer
XCRUN_DIR : /usr/bin
XCODE_AGREED_VERSION : 7.1.1
ISYSROOT : /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
SDKVER : 10.11
MATLABROOT : /Applications/MATLAB_R2014b.app
ARCH : maci64
SRC : /path/to/file/mex.cpp
OBJ : /var/folders/09/dysbhb1d5zn98sq8lzkwpn5r0000gn/T//mex_291104578803706_45753/mex.o
OBJS : /var/folders/09/dysbhb1d5zn98sq8lzkwpn5r0000gn/T//mex_291104578803706_45753/mex.o
SRCROOT : /path/to/file/mex
DEF : /var/folders/09/dysbhb1d5zn98sq8lzkwpn5r0000gn/T//mex_291104578803706_45753/mex.def
EXP : mex.exp
LIB : mex.lib
EXE : mex.mexmaci64
ILK : mex.ilk
MANIFEST : mex.mexmaci64.manifest
TEMPNAME : mex
EXEDIR :
EXENAME : mex
OPTIM : -O2 -DNDEBUG
LINKOPTIM : -O
-------------------------------------------------------------------
Building with 'Xcode Clang++'.
/usr/bin/xcrun -sdk macosx10.11 clang++ -c -DMX_COMPAT_32 -DMATLAB_MEX_FILE -I"/Applications/MATLAB_R2014b.app/extern/include" -I"/Applications/MATLAB_R2014b.app/simulink/include" -L/usr/local/lib -lfftw3 -O2 -DNDEBUG /path/to/file/mex.cpp -o /var/folders/09/dysbhb1d5zn98sq8lzkwpn5r0000gn/T//mex_291104578803706_45753/mex.o
Error using mex
clang: warning: -lfftw3: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/local/lib'
/path/to/file/mex.cpp:4:10:
fatal error: 'fftw3.h' file not found
#include "fftw3.h"
^
1 error generated.
EDIT:
the paths to the library files are
/usr/local/lib/libfftw3.a /usr/local/lib/libfftw3.la
I did try to symbolically link them to end in .so or .dylib but nothing helps

Best Answer

I also had to add the include path which is
-I/usr/local/include/
so now
mex -v -L/usr/local/lib -lfftw3 -I/usr/local/include/ mex.cpp
compiles successfully
Related Question