Home MATLAB mex linking error using clang and external library fftw3
Reply: 1

MATLAB mex linking error using clang and external library fftw3

Benjamin Maier
1#
Benjamin Maier Published in 2018-01-11 14:24:42Z

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

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

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));
}

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.
Cris Luengo
2#
Cris Luengo Reply to 2018-01-11 17:41:58Z

The warnings

clang: warning: -lfftw3: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/local/lib'

are there because you are setting CXXFLAGS to contain these flags. This variable is for compiler flags, but -L and -l are linker flags. Simply remove the CXXFLAGS definition from your mex command.

The error

/path/to/file/mex.cpp:4:10:
fatal error: 'fftw3.h' file not found
#include "fftw3.h"
         ^
1 error generated.

is because the compiler cannot find the fftw4.h header file. You need to tell the compiler where to find it:

mex -I/usr/local/include -L/usr/local/lib -lfftw3 mex.cpp

Note that in your first case, where you call clang++ directly, it is likely that CLang is configured to look for header files in /usr/local/include, it's a standard place. But when mex invokes your compiler (I don't even know if it's the same one?) apparently this location is not added to the search path.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304165 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO